java数据库编程——第七章,课后作业

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

 

你可能感兴趣的:(数据库编程)