遍历结果集
数据库:
第一种方法(int):
package jdbc.chap05;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import jdbc.util.DbUtil;
public class sec01 {
private static DbUtil dbUtil=new DbUtil();
/**
* 遍历查询结果
* @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("========================================================");
}
}
public static void main(String[] args) throws Exception {
listPerson();
}
}
package jdbc.chap05;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import jdbc.util.DbUtil;
public class sec01 {
private static DbUtil dbUtil=new DbUtil();
/**
* 遍历查询结果
* @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("========================================================");
}
}
public static void main(String[] args) throws Exception {
listPerson();
}
}
第二种方法(可读性强):
package jdbc.chap05;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import jdbc.util.DbUtil;
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()){
<span style="color:#ff0000;">int id=rs.getInt(1);//获取第一个列的值 编号ID
String name=rs.getString(2);//获取第二个列的值 编号 name
int age=rs.getInt(3);//获取第三</span>列的值 编号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("========================================================");
}
}
public static void main(String[] args) throws Exception {
// listPerson();
listPerson2();
}
}
结果一样:
学生编号: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
========================================================