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

程序功能:控制台输入车辆信息,将信息保存至数据库中 

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;
}

}

 

上一篇:Problem F: 汽车、客车、货车


下一篇:思博伦PosApp(Positioning Application)使用方法——如何设置动态载具