jdbc实现增删改查

JDBC增删改查

代码展示:

实体类:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SelectALL {
    // 编号
    private int id;
    // 姓名
    private String name;
    // 性别
    private int gender;
    // 入职时间
    private Date createTime;
    // 工资
    private Double salary;
    // 修改时间
    private Date updateTime;
    // 部门
    private  String departName;

}

1、查找

public List<SelectALL> select() throws SQLException {
        // 创建连接
        conn = JbConnection.getConn();
        // sql语句
        String sql = "select * from employee, department where dept_id = department.id order by employee.id; ";
        // 获取预编译对象
        ps = conn.prepareStatement(sql);
        // 执行sql,获取结果集
        rs = ps.executeQuery();
        List<SelectALL> list = new ArrayList<>();
        // 处理结果集
        while(rs.next()){
            SelectALL e = new SelectALL();
            // 编号
            e.setId(rs.getInt(1));
            // 用户名
            e.setName(rs.getString(2));
            // 性别
            e.setGender(rs.getInt(3));
            // 入职时间
            e.setCreateTime(rs.getDate(5));
            // 修改时间
            e.setUpdateTime(rs.getDate(6));
            // 工资
            e.setSalary(rs.getDouble(7));
            // 部门
            e.setDepartName(rs.getString(9));
            list.add(e);
        }
        conn.close();
        return list;
    }

2、分页查找

public List<SelectALL> pageBean(int begin, int pageSize) throws SQLException {
        // 创建连接
        conn = JbConnection.getConn();
        // sql语句
        String sql = "select * from employee, department where dept_id = department.id order by employee.id limit ?, ? ";
        // 获取预编译对象
        ps = conn.prepareStatement(sql);
        ps.setInt(1, begin);
        ps.setInt(2, pageSize);
        // 执行sql,获取结果集
        rs = ps.executeQuery();
        List<SelectALL> list = new ArrayList<>();
        while(rs.next()){
            SelectALL e = new SelectALL();
            // 编号
            e.setId(rs.getInt(1));
            // 用户名
            e.setName(rs.getString(2));
            // 性别
            e.setGender(rs.getInt(3));
            // 入职时间
            e.setCreateTime(rs.getDate(5));
            // 修改时间
            e.setUpdateTime(rs.getDate(6));
            // 工资
            e.setSalary(rs.getDouble(7));
            // 部门
            e.setDepartName(rs.getString(9));
            list.add(e);

        }
        return list;
    }

3、添加员工

 public boolean add(Employee employee) throws SQLException {
        // 创建连接
        conn = JbConnection.getConn();
        // sql语句
        String sql = "insert into employee(name, gender, dept_id, create_time, update_time, salary) values (?, ?, ?, ?, ?, ?)";
        // 获取预编译对象
        ps = conn.prepareStatement(sql);
        // 添加查询属性
        ps.setString(1, employee.getName());
        ps.setInt(2, employee.getGender());
        ps.setInt(3, employee.getDeptId());
        ps.setDate(4, new Date(new java.util.Date().getTime()));
        ps.setDate(5, new Date(new java.util.Date().getTime()));
        ps.setDouble(6, employee.getSalary());
        int p = ps.executeUpdate();
        return p != 0;
    }

4、修改员工信息

public boolean update(Employee employee, int id) throws SQLException {
        // 创建连接
        conn = JbConnection.getConn();
        // sql语句
        String sql = "update employee set name = ?, dept_id = ?,create_time = ?, update_time = ?, salary = ? where id = ?";
        // 获取预编译对象
        ps = conn.prepareStatement(sql);
        // 添加修改元素
        ps.setString(1, employee.getName());
        ps.setInt(2, employee.getDeptId());
        ps.setDate(3, (Date) employee.getCreateTime());
        ps.setDate(4, (Date) employee.getUpdateTime());
        ps.setDouble(5, employee.getSalary());
        ps.setInt(6, id);
        int p = ps.executeUpdate();
        return p != 0;
    }

5、删除员工信息

 public boolean delete(int id) throws SQLException {
        // 建立连接
        conn = JbConnection.getConn();
        // sql语句
        String sql = "delete from employee where id = ?";
        // 添加删除属性
        ps = conn.prepareStatement(sql);
        ps.setInt(1, id);
        int p = ps.executeUpdate();
        return p != 0;
    }

6、模糊查询

public List<SelectALL> findName(String name) throws SQLException {
        // 建立连接
        conn = JbConnection.getConn();
        // sql语句
        String sql = "select * from employee, department " +
                "where dept_id = department.id and " +
                "(employee.name like concat('%', ?, '%'))";
        // 预编译
        ps = conn.prepareStatement(sql);
        ps.setString(1, name);
        // 获取结果集
        rs = ps.executeQuery();
        List<SelectALL> list = new ArrayList<>();
        while(rs.next()) {
            SelectALL e = new SelectALL();
            // 编号
            e.setId(rs.getInt(1));
            // 用户名
            e.setName(rs.getString(2));
            // 性别
            e.setGender(rs.getInt(3));
            // 入职时间
            e.setCreateTime(rs.getDate(5));
            // 修改时间
            e.setUpdateTime(rs.getDate(6));
            // 工资
            e.setSalary(rs.getDouble(7));
            // 部门
            e.setDepartName(rs.getString(9));
            list.add(e);
        }
        return list;
    }

img

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值