package dao.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import util.DBConnection; import dao.EmployeeDAO; import entity.Employee; public class EmployeeDAOJdbcImpl implements EmployeeDAO { public List<Employee> findALL() throws Exception { ArrayList<Employee> list = new ArrayList<Employee>(); Connection conn = DBConnection.getConnection(); PreparedStatement ps = conn.prepareStatement("select * from t_emp"); ResultSet rs = ps.executeQuery(); while(rs.next()) { Employee e = new Employee(); e.setId(rs.getInt("id")); e.setName(rs.getString("name")); e.setAge(rs.getInt("age")); e.setSalary(rs.getDouble("salary")); list.add(e); } DBConnection.close(rs, ps, conn); return list; } public void insertEmp(Employee e) throws Exception { Connection conn = DBConnection.getConnection(); PreparedStatement ps = conn.prepareStatement("insert into t_emp(name,age,salary) values(?,?,?)"); ps.setString(1, e.getName()); ps.setInt(2,e.getAge()); ps.setDouble(3, e.getSalary()); ps.executeUpdate(); System.out.println("插入成功"); DBConnection.close(ps, conn); } public void deleteEmpById(int id) throws Exception { Connection conn = DBConnection.getConnection(); PreparedStatement ps = conn.prepareStatement("delete from t_emp where id=?"); ps.setInt(1,id); ps.executeUpdate(); DBConnection.close(ps, conn); } public void updateEmp(Employee e) throws Exception { Connection conn = DBConnection.getConnection(); PreparedStatement ps = conn.prepareStatement("update t_emp set name=?,age=?,salary=? where id=?"); ps.setString(1, e.getName()); ps.setInt(2,e.getAge()); ps.setDouble(3, e.getSalary()); ps.setInt(4, e.getId()); ps.executeUpdate(); DBConnection.close(ps, conn); } public Employee findEmpById(int id) throws Exception { Employee e = null; Connection conn = DBConnection.getConnection(); PreparedStatement ps = conn.prepareStatement("select * from t_emp where id=?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); while(rs.next()) { e = new Employee(); e.setId(rs.getInt("id")); e.setName(rs.getString("name")); e.setAge(rs.getInt("age")); e.setSalary(rs.getDouble("salary")); } DBConnection.close(rs, ps, conn); return e; } }
最近下载更多
Guo_9981 LV1
4月20日
bai fei liu LV1
3月28日
m5433660 LV1
3月26日
blueskymax LV1
3月5日
GerryGim LV4
1月15日
bjlabc LV1
1月4日
微信网友_6806708913754112 LV1
1月3日
无敌最俊朗啊 LV2
2023年12月29日
597606384 LV1
2023年12月27日
asddwh LV12
2023年12月26日
最近浏览更多
Guo_9981 LV1
4月20日
dhcpsl LV2
4月10日
bai fei liu LV1
3月28日
20201202038
3月27日
暂无贡献等级
m5433660 LV1
3月26日
ruo12138 LV1
3月24日
码农的烦恼
3月19日
暂无贡献等级
vluobo LV1
3月19日
blueskymax LV1
3月5日
qq1633 LV6
2月19日