JDBC--Result 获取返回集合

时间:2022-04-09 19:45:20

package jdbc.chap05;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import jdbc.util.DbUtil;
import model.Person;

public class sec01 {

private static DbUtil dbUtil=new DbUtil();
/**
* 遍历查询结果1
* @throws Exception
*/
private static void listPerson()throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="select * from t_student";
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();//返回2维结果集ResultSet
while (rs.next()){
int id=rs.getInt(1);//获取第一个列的值 编号ID
String name=rs.getString(2);//获取第二个列的值 编号 name
int age=rs.getInt(3);//获取第三列的值 编号age
System.out.println("学生编号:"+id+"学生姓名:"+name+"学生年龄:"+age);
System.out.println("========================================================");

}
}
/**
* 遍历查询结果2
* @throws Exception
*/
private static void listPerson2()throws Exception{
Connection con=dbUtil.getCon();//获取连接
String sql="select * from t_student";
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();//返回2维结果集ResultSet
while (rs.next()){
int id=rs.getInt("id");//获取第一个列的值 编号ID
String name=rs.getString("name");//获取第二个列的值 编号 name
int age=rs.getInt("age");//获取第三列的值 编号age
System.out.println("学生编号:"+id+"学生姓名:"+name+"学生年龄:"+age);
System.out.println("========================================================");

}
}
private static List<Person> listPerson3()throws Exception{
List<Person> personlist=new ArrayList<Person>();
Connection con=dbUtil.getCon();//获取连接
String sql="select * from t_student";
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();//返回2维结果集ResultSet
while (rs.next()){
int id=rs.getInt("id");//获取第一个列的值 编号ID
String personName=rs.getString("name");//获取第二个列的值 编号 name
int age=rs.getInt("age");//获取第三列的值 编号age
Person person=new Person(id, personName, age);
personlist.add(person);


}
return personlist;

}
public static void main(String[] args) throws Exception {

//listPerson();
//listPerson2();
List<Person> personList=listPerson3();
for (Person person:personList){
System.out.println(person.toString());
}
}
}

关键点:重写ToString

package model;
/**
* 个人信息
* @author MC-DS
*
*/

public class Person {

private int id;
private String personName;
private int age;



public Person(int id, String personName, int age) {
super();
this.id = id;
this.personName = personName;
this.age = age;
}
public Person(String personName, int age) {
super();
this.personName = personName;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Integer getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "["+this.id+","+this.personName+","+this.age+"]";
}

}

写前:
model.Person@27d43d30
model.Person@5efd2ebd
model.Person@4007ab03
model.Person@376c72cc
model.Person@30e4cb81
model.Person@7cec9b3a
model.Person@11c33ce9
model.Person@28d3ee1b
model.Person@71b5438d
model.Person@3366184d
model.Person@73c58197
model.Person@2bbf1be2

写后:

[1,ling,18]
[2,李小龍,18]
[6,劉德華,17]
[7,古天樂,37]
[8,李小龍,18]
[22,李小龍,18]
[222,李小龍,18]
[233,李小龍,18]
[656,李小龍,18]
[658,李小龍,18]
[659,郑伊健,37]
[661,陈小春,32]