1. package jdbc.chap05;
2.
3. import java.sql.Connection;
4. import java.sql.PreparedStatement;
5. import java.sql.ResultSet;
6. import java.util.ArrayList;
7. import java.util.List;
8.
9. import jdbc.util.DbUtil;
10. import model.Person;
11.
12. public class sec01 {
13.
14. private static DbUtil dbUtil=new DbUtil();
15. /**
16. * 遍历查询结果1
17. * @throws Exception
18. */
19. private static void listPerson()throws Exception{
20. //获取连接
21. "select * from t_student";
22. PreparedStatement pstmt=con.prepareStatement(sql);
23. //返回2维结果集ResultSet
24. while (rs.next()){
25. int id=rs.getInt(1);//获取第一个列的值 编号ID
26. 2);//获取第二个列的值 编号 name
27. int age=rs.getInt(3);//获取第三列的值 编号age
28. "学生编号:"+id+"学生姓名:"+name+"学生年龄:"+age);
29. "========================================================");
30.
31. }
32. }
33. /**
34. * 遍历查询结果2
35. * @throws Exception
36. */
37. private static void listPerson2()throws Exception{
38. //获取连接
39. "select * from t_student";
40. PreparedStatement pstmt=con.prepareStatement(sql);
41. //返回2维结果集ResultSet
42. while (rs.next()){
43. int id=rs.getInt("id");//获取第一个列的值 编号ID
44. "name");//获取第二个列的值 编号 name
45. int age=rs.getInt("age");//获取第三列的值 编号age
46. "学生编号:"+id+"学生姓名:"+name+"学生年龄:"+age);
47. "========================================================");
48.
49. }
50. }
51. private static List<Person> listPerson3()throws Exception{
52. new ArrayList<Person>();
53. //获取连接
54. "select * from t_student";
55. PreparedStatement pstmt=con.prepareStatement(sql);
56. //返回2维结果集ResultSet
57. while (rs.next()){
58. int id=rs.getInt("id");//获取第一个列的值 编号ID
59. "name");//获取第二个列的值 编号 name
60. int age=rs.getInt("age");//获取第三列的值 编号age
61. new Person(id, personName, age);
62. personlist.add(person);
63.
64.
65. }
66. return personlist;
67.
68. }
69. public static void main(String[] args) throws Exception {
70.
71. // listPerson();
72. // listPerson2();
73. List<Person> personList=listPerson3();
74. for (Person person:personList){
75. System.out.println(person.toString());
76. }
77. }
78. }
关键点:重写ToString
1. package model;
2. /**
3. * 个人信息
4. * @author MC-DS
5. *
6. */
7.
8. public class Person {
9.
10. private int id;
11. private String personName;
12. private int age;
13.
14.
15.
16. public Person(int id, String personName, int age) {
17. super();
18. this.id = id;
19. this.personName = personName;
20. this.age = age;
21. }
22. public Person(String personName, int age) {
23. super();
24. this.personName = personName;
25. this.age = age;
26. }
27. public int getId() {
28. return id;
29. }
30. public void setId(int id) {
31. this.id = id;
32. }
33. public String getPersonName() {
34. return personName;
35. }
36. public void setPersonName(String personName) {
37. this.personName = personName;
38. }
39. public Integer getAge() {
40. return age;
41. }
42. public void setAge(int age) {
43. this.age = age;
44. }
45. @Override
46. public String toString() {
47. // TODO Auto-generated method stub
48. return "["+this.id+","+this.personName+","+this.age+"]";
49. }
50.
51. }
写前:
1. model.Person@27d43d30
2. model.Person@5efd2ebd
3. model.Person@4007ab03
4. model.Person@376c72cc
5. model.Person@30e4cb81
6. model.Person@7cec9b3a
7. model.Person@11c33ce9
8. model.Person@28d3ee1b
9. model.Person@71b5438d
10. model.Person@3366184d
11. model.Person@73c58197
12. model.Person@2bbf1be2
写后:
1. [1,ling,18]
2. [2,李小龍,18]
3. [6,劉德華,17]
4. [7,古天樂,37]
5. [8,李小龍,18]
6. [22,李小龍,18]
7. [222,李小龍,18]
8. [233,李小龍,18]
9. [656,李小龍,18]
10. [658,李小龍,18]
11. [659,郑伊健,37]
12. [661,陈小春,32]