![]() 图 错误!文档中没有指定样式的文字。EMPLOYEE表的数据 |
| 1. package chapter25.db; 2. public class Employee 3. { 4. private String id; 5. private String name; 6. private int age; 7. public Employee(String id, String name, int age) { 8. this.id = id; 9. this.name = name; 10. this.age = age; 11. } 12. public String getId() { 13. return id; 14. } 15. public String getName() { 16. return name; 17. } 18. public int getAge() { 19. return age; 20. } 21. public boolean equals(Object o) { 22. if (o instanceof Employee) { 23. Employee e1 = (Employee) o; 24. return id.equals(e1.getId()) && name.equals(e1.getName()) &&age == e1.getAge(); 25. } else { 26. return false; 27. } 28. } 29. } |
| 1. package chapter25.db; 2. import java.sql.*; 3. public class EmployeeDAO 4. { 5. private Connection conn; 6. public EmployeeDAO(Connection conn) { 7. this.conn = conn; 8. } 9. public Employee findById(String id) throws SQLException 10. { 11. String sqlStr = "select * from employee where id ='"+id+"'"; 12. Statement stat = conn.createStatement(); 13. ResultSet rs = stat.executeQuery(sqlStr); 14. if (rs.next()) { 15. return new Employee(id,rs.getString("name"),rs.getInt("age")); 16. }else{ 17. return null; 18. } 19. } 20. } |
![]() 图 错误!文档中没有指定样式的文字。指定JDBC测试固件类名 |