直接上代码,一个实现FundDao接口的类
public class FundDaoJDBCImpl implements FundDao {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
/**
* 查找Fund表的所有数据
*
* @return 返回List
*/
@Override
public List findAll() {
ArrayList<Fund> funds = new ArrayList<>();
Fund fund = null;
//1. 获取连接
conn = JDBCUtil.getConnection();
//2. 写SQL语句
String sql = "SELECT * FROM fund";
//3. 创建PreparedStatement
try {
pstmt = conn.prepareStatement(sql);
//4 .发送执行sql语句,得到结果集
rs = pstmt.executeQuery();
//5. 对结果集遍历
while (rs.next()) {
Integer id = rs.getInt("id");
String name = rs.getString("name");
String des = rs.getString("description");
Double price = rs.getDouble("price");
String status = rs.getString("status");
Date createTime = rs.getDate("createTime");
fund = new Fund(id, name, price, des, status, createTime);
funds.add(fund);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//6. 关闭连接对象等
JDBCUtil.close(rs, pstmt, conn);
}
return funds;
}
/**
* 向fund表插入一条数据
*
* @param fund Fund对象
*/
@Override
public void insert(Fund fund) {
//1. 获取连接
conn = JDBCUtil.getConnection();
//2. 写SQL语句
String sql = "INSERT INTO fund (name,description,price,status,createTime) VALUE(?,?,?,?,?)";
//3. 创建PreparedStatement
try {
pstmt = conn.prepareStatement(sql);
//4. 给sql变量赋值
pstmt.setString(1, fund.getName());
pstmt.setString(2, fund.getDescription());
pstmt.setDouble(3, fund.getPrice());
pstmt.setString(4, fund.getStatus());
java.sql.Date date = new java.sql.Date(fund.getCreateTime().getTime());
pstmt.setDate(5, date);
//5 .发送执行sql语句,得到结果集
int rus = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//6. 关闭连接对象等
JDBCUtil.close(rs, pstmt, conn);
}
}
/**
* 从fund表中删除一条数据
*
* @param fundNo id值
*/
@Override
public void delete(int fundNo) {
//1. 获取连接
conn = JDBCUtil.getConnection();
//2. 写SQL语句
String sql = "DELETE FROM fund WHERE id = ?";
//3. 创建PreparedStatement
try {
pstmt = conn.prepareStatement(sql);
//4. 给sql变量赋值
pstmt.setInt(1, fundNo);
//5 .发送执行sql语句,得到结果集
int rus = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//6. 关闭连接对象等
JDBCUtil.close(rs, pstmt, conn);
}
}
/**
* 向fund表更新一条数据
*
* @param fund Fund对象
*/
@Override
public void update(Fund fund) {
//1. 获取连接
conn = JDBCUtil.getConnection();
//2. 写SQL语句
String sql = "UPDATE fund SET name=?,price=?,description=?,status=?,createTime=? WHERE id=?";
//3. 创建PreparedStatement
try {
pstmt = conn.prepareStatement(sql);
//4. 给sql变量赋值
pstmt.setString(1, fund.getName());
pstmt.setDouble(2, fund.getPrice());
pstmt.setString(3, fund.getDescription());
pstmt.setString(4, fund.getStatus());
pstmt.setDate(5, new java.sql.Date(fund.getCreateTime().getTime()));
pstmt.setInt(6, fund.getId());
//5 .发送执行sql语句,得到结果集
int rus = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//6. 关闭连接对象等
JDBCUtil.close(rs, pstmt, conn);
}
}
/**
* 通过ID查找数据
*
* @param fundNo 数据id值
* @return Fund对象
*/
@Override
public Fund findById(int fundNo) {
Fund fund = null;
//1. 获取连接
conn = JDBCUtil.getConnection();
//2. 写SQL语句
String sql = "SELECT * FROM fund WHERE id=?";
//3. 创建PreparedStatement
try {
pstmt = conn.prepareStatement(sql);
//4. 给sql变量赋值
pstmt.setInt(1, fundNo);
//5 .发送执行sql语句,得到结果集
rs = pstmt.executeQuery();
//6. 对结果集遍历
while (rs.next()) {
Integer id = rs.getInt("id");
String name = rs.getString("name");
String des = rs.getString("description");
Double price = rs.getDouble("price");
String status = rs.getString("status");
Date createTime = rs.getDate("createTime");
fund = new Fund(id, name, price, des, status, createTime);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//7. 关闭连接对象等
JDBCUtil.close(rs, pstmt, conn);
}
return fund;
}
}