Java对数据库的增删改查

直接上代码,一个实现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;
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值