java資料庫程式設計——第七章,課後作業
阿新 • • 發佈:2019-01-31
1.由控制檯輸入資料,提交後儲存到MYsql資料庫。(使用DAO模式)
package DAO_dome.kehozuoye; /** * 汽車實體類 * @author huang * */ public class Vehicie { private String identity;//車主身份證號碼 private String heading;//車輛識別碼 private double emissions;//車輛排量 private double price;//官方指導價 private double invoice;//發票價格 private double purchase;//繳納車輛購稅價 public String getIdentity() { return identity; } public void setIdentity(String identity) { this.identity = identity; } public String getHeading() { return heading; } public void setHeading(String heading) { this.heading = heading; } public double getEmissions() { return emissions; } public void setEmissions(double emissions) { this.emissions = emissions; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getInvoice() { return invoice; } public void setInvoice(double invoice) { this.invoice = invoice; } public double getPurchase() { return purchase; } public void setPurchase(double purchase) { this.purchase = purchase; } }
package DAO_dome.kehozuoye; /** * 汽車Vehicie介面 * @author huang * */ import java.util.List; public interface VehicieDao { /** * 儲存汽車 * @param vehicie * @return */ int save(Vehicie vehicie); /** * 刪除資訊 * @param vehicie * @return */ int del(Vehicie vehicie); /** * 更新汽車 * @param vehicie * @return */ int Update(Vehicie vehicie); /** * 獲取汽車識別程式碼列表,模糊查詢 * @param heading * @return */ Vehicie getByName(String heading); /** * 獲取汽車識別程式碼列表,精確查詢 * @param heading * @return */ List<Vehicie> findByName(String heading); }
package DAO_dome.kehozuoye; import java.util.List; /** * Vehicie針對MySQL資料庫的實現類 * @author huang * */ public class VehicieDaoMysql extends BaseDao implements VehicieDao{ @Override public int save(Vehicie vehicie) { // TODO Auto-generated method stub String sql = "insert into vehicle(identity,heading,emissions,price,invoice,purchase) values(?,?,?,?,?,?)"; Object[] param = {vehicie.getIdentity(),vehicie.getHeading(),vehicie.getEmissions(), vehicie.getPrice(),vehicie.getIdentity(),vehicie.getPurchase()}; int result = this.executeUpdate(sql, param); return result; } @Override public int del(Vehicie vehicie) { // TODO Auto-generated method stub return 0; } @Override public int Update(Vehicie vehicie) { // TODO Auto-generated method stub return 0; } @Override public Vehicie getByName(String heading) { // TODO Auto-generated method stub return null; } @Override public List<Vehicie> findByName(String heading) { // TODO Auto-generated method stub System.out.println(); return null; } public void add() { System.out.println("你是豬嗎"); } static { System.out.println("im,true!"); } } class jb{ public void sb() { boolean is=false; if(is) { System.out.println("do"); }else { System.out.println("ys"); } } }
package DAO_dome.kehozuoye;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 資料庫操作通用類
* @author huang
*
*/
public class BaseDao {
private String driver = "com.mysql.jdbc.Driver";//資料庫驅動字串
private String url = "jdbc:mysql://localhost:3306/zoology";//資料庫連線字串
private String user = "root";//資料庫使用者名稱
private String password = "123135";//資料庫密碼
//開啟資料庫方法
Connection con = null;
public Connection getConnection() {
//載入驅動
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//連線資料庫
try {
con = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//關閉資源方法
public void CloseAll(Connection con,PreparedStatement pre,ResultSet result) {
if(result!=null) {
try {
result.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pre!=null) {
try {
pre.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//增刪改查通用方法
public int executeUpdate(String sql,Object[] param) {
int num = 0;//影響行數
con = this.getConnection();
PreparedStatement pre = null;
try {
pre = con.prepareStatement(sql);
//為引數賦值
if(param!=null) {
for(int i=0;i<param.length;i++) {
pre.setObject(i+1, param[i]);
}
}
//執行sql語句
num = pre.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
this.CloseAll(con, pre, null);
}
return num;
}
}
package DAO_dome.kehozuoye;
import java.util.Scanner;
/**
* 測試類
* @author huang
*
*/
public class Test {
public static void main(String[] args) {
VehicieDao vehicieDao = new VehicieDaoMysql();
Vehicie vehicie = new Vehicie();
String identity = "";//車主身份證號碼
String heading = "";//車輛識別碼
double emissions = 0;//車輛排量
double price = 0.0;//官方指導價
double invoice = 0.0;//發票價格
double purchase = 0.0;//繳納車輛購稅價
double purchasePrice = 0.0;//計稅價格
Scanner scanner = new Scanner(System.in);
System.out.println("記錄車輛購置稅,請按提示錄入相關資訊:");
System.out.println("請輸入車主身份證號碼(18位):");
identity = scanner.next();
while(identity.length()!=18) {
System.out.println("輸入錯誤!請重新輸入:");
identity = scanner.next();
}
System.out.println("請輸入車輛識別碼(17位):");
heading = scanner.next();
while(heading.length()!=17) {
System.out.println("輸入錯誤!請重新輸入:");
heading = scanner.next();
}
System.out.println("請輸入車輛排量:");
emissions = scanner.nextDouble();
System.out.println("請輸入官方指導價:");
price = scanner.nextInt();
System.out.println("請輸入發票價格:");
invoice = scanner.nextInt();
purchasePrice = invoice/(1+0.17);
if(emissions>1.6) {
purchase = purchasePrice*0.1;
}else {
purchase = purchasePrice*0.075;
}
vehicie.setIdentity(identity);
vehicie.setHeading(heading);
vehicie.setEmissions(emissions);
vehicie.setPrice(price);
vehicie.setInvoice(invoice);
vehicie.setPurchase(purchase);
vehicieDao.save(vehicie);
System.out.println("資料包存成功,車輛購置稅為"+purchase);
}
}