JDBC----Result结果集

来源:互联网 发布:人工智能发展现状2017 编辑:程序博客网 时间:2024/05/16 17:17

遍历结果集

数据库:

第一种方法(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维结果集ResultSetwhile (rs.next()){int id=rs.getInt(1);//获取第一个列的值 编号IDString name=rs.getString(2);//获取第二个列的值 编号 nameint age=rs.getInt(3);//获取第三列的值 编号ageSystem.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维结果集ResultSetwhile (rs.next()){<span style="color:#ff0000;">int id=rs.getInt(1);//获取第一个列的值 编号IDString name=rs.getString(2);//获取第二个列的值 编号 nameint age=rs.getInt(3);//获取第三</span>列的值 编号ageSystem.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维结果集ResultSetwhile (rs.next()){int id=rs.getInt("id");//获取第一个列的值 编号IDString name=rs.getString("name");//获取第二个列的值 编号 nameint age=rs.getInt("age");//获取第三列的值 编号ageSystem.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========================================================


0 0
原创粉丝点击