日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java:控制台输入车辆信息,将信息保存至数据库中

發布時間:2023/12/13 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java:控制台输入车辆信息,将信息保存至数据库中 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

程序功能:控制臺輸入車輛信息,將信息保存至數據庫中?

程序代碼如下:

BaseDao.java

?

package DAO_dome.kehozuoye;

?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private String driver = "com.mysql.jdbc.Driver";//數據庫驅動字符串
private String url = "jdbc:mysql://localhost:3306/zoology?useUnicode=true&characterEncoding=utf8";
private String user = "root";//數據庫用戶名
private String password = "duanqibo919";//數據庫密碼
//打開數據庫方法
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;
}

?

}

?

Test.java

?

package DAO_dome.kehozuoye;

?

import java.util.Scanner;
import DAO_dome.kehozuoye.VehicleDao;
import DAO_dome.kehozuoye.VehicleDaoMysql;
public class Test {
public static void main(String[] args) {
VehicleDao vehicleDao = new VehicleDaoMysql();
Vehicle vehicle = new Vehicle();
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.nextDouble();
System.out.println("請輸入發票價格:");
invoice = scanner.nextDouble();
purchasePrice = invoice/(1+0.17);
if(emissions>1.6) {
purchase = purchasePrice*0.1;
}else {
purchase = purchasePrice*0.075;
}
vehicle.setIdentity(identity);
vehicle.setHeading(heading);
vehicle.setEmissions(emissions);
vehicle.setPrice(price);
vehicle.setInvoice(invoice);
vehicle.setPurchase(purchase);
vehicleDao.save(vehicle);
System.out.println("數據保存成功,車輛購置稅為"+purchase);

scanner.close();
}

?

}

?

Vehicle.java

?

package DAO_dome.kehozuoye;

?

?

?

public class Vehicle {

?

/*

?

* 汽車實體類

?

*/

?

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;

?

}

?

}

?

VehicleDao.java ? ? //聲明各種接口

?

package DAO_dome.kehozuoye;

?

import java.util.List;
import DAO_dome.kehozuoye.Vehicle;

?


public interface VehicleDao {
/*
* 汽車接口
*/
/**
* 保存汽車
* @param vehicie
* @return
*/
int save(Vehicle vehicle);
/**
* 刪除信息
* @param vehicie
* @return
*/
int del(Vehicle vehicle);
/**
* 更新汽車
* @param vehicie
* @return
*/
int Update(Vehicle vehicle);
/**
* 獲取汽車識別代碼列表,模糊查找
* @param heading
* @return
*/
Vehicle getByName(String heading);
/**
* 獲取汽車識別代碼列表,精確查找
* @param heading
* @return
*/
List<Vehicle> findByName(String heading);

?

}

?

?

?

?

VehicleDaoMysql.java ? //上述接口的實現

?

package DAO_dome.kehozuoye;

?

import java.util.List;
import DAO_dome.kehozuoye.BaseDao;
import DAO_dome.kehozuoye.VehicleDao;
import DAO_dome.kehozuoye.Vehicle;
public class VehicleDaoMysql extends BaseDao implements VehicleDao{

@Override
public int save(Vehicle vehicle) {
// TODO Auto-generated method stub
String sql = "insert into vehicle(identity,heading,emissions,price,invoice,purchase) "
+ "values(?,?,?,?,?,?)";
Object[] param = {vehicle.getIdentity(),vehicle.getHeading(),vehicle.getEmissions(),
vehicle.getPrice(),vehicle.getInvoice(),vehicle.getPurchase()};
int result = this.executeUpdate(sql, param);
return result;
}

?

@Override
public int del(Vehicle vehicle) {
// TODO Auto-generated method stub
return 0;
}

?

@Override
public int Update(Vehicle vehicle) {
// TODO Auto-generated method stub
return 0;
}

?

@Override
public Vehicle getByName(String heading) {
// TODO Auto-generated method stub
return null;
}

?

@Override
public List<Vehicle> findByName(String heading) {
// TODO Auto-generated method stub


System.out.println();
return null;
}

}

?

轉載于:https://www.cnblogs.com/duanqibo/p/11103413.html

總結

以上是生活随笔為你收集整理的Java:控制台输入车辆信息,将信息保存至数据库中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。