01 | package dao.jdbc; |
02 |
03 | import java.sql.Connection; |
04 | import java.sql.PreparedStatement; |
05 | import java.sql.ResultSet; |
06 | import java.util.ArrayList; |
07 | import java.util.List; |
08 | import util.DBConnection; |
09 | import dao.EmployeeDAO; |
10 | import entity.Employee; |
11 |
12 | public class EmployeeDAOJdbcImpl implements EmployeeDAO { |
13 |
14 | public List<Employee> findALL() throws Exception { |
15 | ArrayList<Employee> list = new ArrayList<Employee>(); |
16 | Connection conn = DBConnection.getConnection(); |
17 | PreparedStatement ps = conn.prepareStatement( "select * from t_emp" ); |
18 | ResultSet rs = ps.executeQuery(); |
19 | |
20 | while (rs.next()) { |
21 | Employee e = new Employee(); |
22 | e.setId(rs.getInt( "id" )); |
23 | e.setName(rs.getString( "name" )); |
24 | e.setAge(rs.getInt( "age" )); |
25 | e.setSalary(rs.getDouble( "salary" )); |
26 | list.add(e); |
27 | } |
28 | DBConnection.close(rs, ps, conn); |
29 | return list; |
30 | } |
31 | |
32 | public void insertEmp(Employee e) throws Exception { |
33 | Connection conn = DBConnection.getConnection(); |
34 | PreparedStatement ps = conn.prepareStatement( "insert into t_emp(name,age,salary) values(?,?,?)" ); |
35 | ps.setString( 1 , e.getName()); |
36 | ps.setInt( 2 ,e.getAge()); |
37 | ps.setDouble( 3 , e.getSalary()); |
38 | ps.executeUpdate(); |
39 | System.out.println( "插入成功" ); |
40 | DBConnection.close(ps, conn); |
41 | |
42 | } |
43 | |
44 | public void deleteEmpById( int id) throws Exception { |
45 | Connection conn = DBConnection.getConnection(); |
46 | PreparedStatement ps = conn.prepareStatement( "delete from t_emp where id=?" ); |
47 | ps.setInt( 1 ,id); |
48 | ps.executeUpdate(); |
49 | |
50 | DBConnection.close(ps, conn); |
51 | } |
52 | |
53 | public void updateEmp(Employee e) throws Exception { |
54 | Connection conn = DBConnection.getConnection(); |
55 | PreparedStatement ps = conn.prepareStatement( "update t_emp set name=?,age=?,salary=? where id=?" ); |
56 | ps.setString( 1 , e.getName()); |
57 | ps.setInt( 2 ,e.getAge()); |
58 | ps.setDouble( 3 , e.getSalary()); |
59 | ps.setInt( 4 , e.getId()); |
60 | ps.executeUpdate(); |
61 | |
62 | DBConnection.close(ps, conn); |
63 | } |
64 | |
65 | public Employee findEmpById( int id) throws Exception { |
66 | Employee e = null ; |
67 | Connection conn = DBConnection.getConnection(); |
68 | PreparedStatement ps = conn.prepareStatement( "select * from t_emp where id=?" ); |
69 | ps.setInt( 1 , id); |
70 | ResultSet rs = ps.executeQuery(); |
71 | |
72 | while (rs.next()) { |
73 | e = new Employee(); |
74 | e.setId(rs.getInt( "id" )); |
75 | e.setName(rs.getString( "name" )); |
76 | e.setAge(rs.getInt( "age" )); |
77 | e.setSalary(rs.getDouble( "salary" )); |
78 | } |
79 | DBConnection.close(rs, ps, conn); |
80 | return e; |
81 | |
82 | } |
83 |
84 |
85 | |
86 | } |

山晴多 LV1
6月6日
2022102154 LV1
4月3日
小橘子是你啊 LV1
3月31日
微信网友_7399890514677760 LV1
2月25日
新哥新奇士橙 LV4
1月26日
cxdddd LV1
2024年12月31日
sinuodeng123 LV5
2024年12月13日
jiafei LV1
2024年12月11日
b1uccc LV2
2024年12月8日
blank1 LV1
2024年11月28日