1. 程式人生 > >第七章 DAO模式

第七章 DAO模式

1,開發一個程式,用於記錄車輛購置稅

package com.zuoye.demo;

/**
 * 實體類車輛資訊
 * 
 * @author 66479
 *
 */
public class Car {
	private String Owneridentitycard;// 車主身份證號碼
	private String Vehicleidentificationcode;// 車輛識別號碼
	private double displacement;// 車輛排量
	private double Officialguidanceprice;// 官方指導價
	private double Invoiceprice;// 發票價格
	private int Purchasetax;// 車輛購置稅

	public String getOwneridentitycard() {
		return Owneridentitycard;
	}

	public void setOwneridentitycard(String owneridentitycard) {
		Owneridentitycard = owneridentitycard;
	}

	public String getVehicleidentificationcode() {
		return Vehicleidentificationcode;
	}

	public void setVehicleidentificationcode(String vehicleidentificationcode) {
		Vehicleidentificationcode = vehicleidentificationcode;
	}

	public double getDisplacement() {
		return displacement;
	}

	public void setDisplacement(double displacement) {
		this.displacement = displacement;
	}

	public double getOfficialguidanceprice() {
		return Officialguidanceprice;
	}

	public void setOfficialguidanceprice(Double officialguidanceprice) {
		Officialguidanceprice = officialguidanceprice;
	}

	public double getInvoiceprice() {
		return Invoiceprice;
	}

	public void setInvoiceprice(Double invoiceprice) {
		Invoiceprice = invoiceprice;
	}

	public int getPurchasetax() {
		return Purchasetax;
	}

	public void setPurchasetax(int purchasetax) {
		Purchasetax = purchasetax;
	}

}
package com.zuoye.demo;
/**
 * 車的介面
 * @author 66479
 *
 */
public interface CarDao {
	  int add(Car car);
}
package com.zuoye.demo;
/**
 * 介面實現類
 * @author 66479
 *
 */
public class carDAOlImpl extends BaseDao implements CarDao{
	public int add(Car car) {
		String sql="insert into car (Owneridentitycard,Vehicleidentificationcode,displacement,Officialguidanceprice,Invoiceprice,Purchasetax)VALUES(?,?,?,?,?,?)";
		int num = executeUpdate(sql, car.getOwneridentitycard(),car.getVehicleidentificationcode(),car.getDisplacement(),car.getOfficialguidanceprice(),car.getInvoiceprice(),car.getPurchasetax());
		return num;
	}
}
package com.zuoye.demo;

import java.io.IOException;
	import java.io.InputStream;
	import java.sql.Connection;
	import java.sql.DriverManager;
	import java.sql.PreparedStatement;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.util.Properties;
/**
 * 工具類
 * @author 66479
 *
 */
	public class BaseDao {
	    private static String driver;
	    private static String url;
	    private static String user;
	    private static String password;
	    Connection conn = null;
	    PreparedStatement stmt=null;
	    
	    static {
	    	into();
	    }
	    public static void into() {
	    	Properties prarms=new Properties();
	    	String config="database.properties";
	    	InputStream is=BaseDao.class.getClassLoader().getResourceAsStream(config);
	    	try {
				prarms.load(is);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	driver=prarms.getProperty("driver");
	    	url=prarms.getProperty("url");
	    	user=prarms.getProperty("userName");
	    	password=prarms.getProperty("password");
	    	
	    	
	    }
	    
	    public Connection getConnection() {
	    	try {
				Class.forName(driver);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	try {
	    		conn=DriverManager.getConnection(url,user,password);
	    		return conn;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	return null;
	    }
	    
	    	public void closeAll(Connection connection,PreparedStatement stmt,ResultSet rs) {
	    		if(rs!=null) {
	    			try {
	    				rs.close();
	    			} catch (SQLException e) {
	    				// TODO Auto-generated catch block
	    				e.printStackTrace();
	    			}
	    		}
	    		if(stmt!=null) {
	    			try {
	    				stmt.close();
	    			} catch (SQLException e) {
	    				// TODO Auto-generated catch block
	    				e.printStackTrace();
	    			}
	    		}
	    		if(conn!=null) {
	    			try {
	    				conn.close();
	    			} catch (SQLException e) {
	    				// TODO Auto-generated catch block
	    				e.printStackTrace();
	    			}
	    		}
	    	}
	    	
	    	public int executeUpdate(String sql,Object...param) {
	    		int num=0;//執行行數
	    		conn=getConnection();
	    		try {
	    			stmt=conn.prepareStatement(sql);
	    			//為引數賦值
	    			if(param!=null) {
	    				for(int i=0;i<param.length;i++) {
	    					stmt.setObject(i+1,param[i]); 
	    				}
	    			}
	    			//執行sql語句
	    			num=stmt.executeUpdate();
	    		} catch (SQLException e) {
	    			// TODO Auto-generated catch block
	    			e.printStackTrace();
	    		}finally {
	    			this.closeAll(conn, stmt, null);
	    		}
	    		return num;
	    	}

	    }
package com.zuoye.demo;
import java.util.Scanner;
/**
 * 測試類
 * @author 66479
 *
 */
public class Test {
   public static void main(String[] args) {
	   Scanner in=new Scanner(System.in);
	   CarDao cardao=new carDAOlImpl();
	   Car car=new Car();
	  System.out.println("記錄車輛購置稅,請按提示錄入相關資訊:");
	  System.out.print("請輸入車主身份證號碼(18位):");
	  String Owneridentitycard=in.next();
	  System.out.print("請輸入車輛識別碼(17位):");
	  String Vehicleidentificationcode=in.next();
	  System.out.print("請輸入車排量:");
	  double displacement=in.nextDouble();
	  System.out.print("請輸入官方指導價:");
	  double Officialguidanceprice=in.nextDouble();
	  System.out.print("請輸入發票價格:");
	  double Invoiceprice=in.nextDouble();
	  int Purchasetax=0;
	  if(displacement>1.6) {
		  Purchasetax=(int) ((Invoiceprice/(1+0.17))*0.1);
	  }else {
		  Purchasetax=(int) ((Invoiceprice/(1+0.17))*0.75);
	  }
	    car.setOwneridentitycard(Owneridentitycard);
		car.setVehicleidentificationcode(Vehicleidentificationcode);
		car.setDisplacement(displacement);
		car.setOfficialguidanceprice(Officialguidanceprice);
		car.setInvoiceprice(Invoiceprice);
		car.setPurchasetax(Purchasetax);
		cardao.add(car);
		System.out.println("資料儲存成功,車輛購置稅為"+Purchasetax);
}
}