最近几天学习了mysql数据库和JDBC基础,之前了解过MVC思想,所以简单修改实现了一个笔记本管理小案例
Model层:笔记本类,持有所有数据和逻辑
import java.util.Date;
public class Laptap {
private int id;
private String name;
private String brand;
private double price;
private Date prodate;
private Date opradate;
public Laptap() {
}
public Laptap(int id, String name, String brand, double price, Date prodate, Date opradate) {
this.id = id;
this.name = name;
this.brand = brand;
this.price = price;
this.prodate = prodate;
this.opradate = opradate;
}
@Override
public String toString() {
return "笔记本:" +
"id=" + id +
", 名称='" + name + '\'' +
", 品牌='" + brand + '\'' +
", 价格=" + price +
", 生产日期=" + prodate +
", 更新日期=" + opradate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Date getProdate() {
return prodate;
}
public void setProdate(Date prodate) {
this.prodate = prodate;
}
public Date getOpradate() {
return opradate;
}
public void setOpradate(Date opradate) {
this.opradate = opradate;
}
}
View层:控制台模拟,用户界面
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class LapView {
public static void main(String[] args) throws ParseException {
LapDao lapDao = new LapDao();
Laptap l1 = new Laptap();
l1.setName("测试");
l1.setBrand("联想");
l1.setPrice(5000);
l1.setProdate(new SimpleDateFormat("yyyy-MM-dd").parse("2018-10-30"));
// lapDao.addLap(l1);
// lapDao.updateLap(1,3500.0);
// lapDao.delLap(1);
lapDao.selectLap();
}
}
Control层:数据库操作类,用户交互,处理数据
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LapDao {
private static final Connection conn = JDBCUtils.getConnection();
/**
* 增加笔记本电脑
*/
public void addLap(Laptap lap) {
PreparedStatement ps = null;
String sql = "insert into laptap values (NULL , ?, ?, ?, ?, NULL)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, lap.getName());
ps.setString(2, lap.getBrand());
ps.setDouble(3, lap.getPrice());
ps.setDate(4, new java.sql.Date(lap.getProdate().getTime()));
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(ps, conn);
}
}
/**
* 删除笔记本
*/
public void delLap(int id) {
PreparedStatement ps = null;
String sql = "DELETE FROM laptap WHERE id = ?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(ps, conn);
}
}
/**
* 更改笔记本信息
*/
public void updateLap(int id, Double price) {
PreparedStatement ps = null;
String sql = "UPDATE laptap SET price = ? WHERE id = ?";
try {
ps = conn.prepareStatement(sql);
ps.setDouble(1, price);
ps.setInt(2, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(ps, conn);
}
}
/**
* 查询笔记本
*/
public void selectLap() {
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "SELECT * FROM laptap";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
System.out.println("编号:"+rs.getInt(1)+", 名称:"+rs.getString(2)+
", 品牌:"+rs.getString(3)+", 价格:"+rs.getDouble(4)+", 生产日期:"+
rs.getDate(5));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, ps, conn);
}
}
}
这是建表语句:
CREATE TABLE laptap (
id INT PRIMARY KEY AUTO_INCREMENT, -- 编号
NAME VARCHAR(64) NOT NULL, -- 名称
brand VARCHAR(64) NOT NULL, -- 品牌
price DECIMAL(6,2) NOT NULL, -- 价格
prodate DATE NOT NULL, -- 生产日期
opradate TIMESTAMP -- 更新日期
)
按照课堂讲解的实现简单JDBCUtils工具类及配置文件就不一一放出了,当然因为还没有学到JDBC连接池,可以用其进行改进,同时可以增加业务复杂度进一步实现一个简单的XXX数据管理系统。 |
|