JDBC--Result 获取返回集合

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]


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值