ResultSet.next()方法

以下是javadoc中的注释:

boolean java.sql.ResultSet.next() throws SQLException

Moves the cursor forward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.

When a call to the next method returns false, the cursor is positioned after the last row. Any invocation of a ResultSet method which requires a current row will result in a SQLException being thrown. If the result set type is TYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation will return false or throw anSQLException on a subsequent call to next.

If an input stream is open for the current row, a call to the method next will implicitly close it. A ResultSet object's warning chain is cleared when a new row is read.

渣翻一下:

将指针移动到当前位置的下一行。ResultSet 指针的初始位置位于第一行之前;第一次调用next()方法将会把第一行设置为当前行;第二次调用next()方法指针移动到第二行,以此类推。

当对next()方法调用返回 false,说明此时指针位于最后一行之后。所有对 ResultSet 需要使用当前行的方法[注:如getString()、getInt()等等]的调用都将导致next()方法抛出 SQLException 异常。如果返回的 ResultSet 集合的类型被设置为 TYPE_FORWARD_ONLY ,会在随后对next()方法的调用中返回 false 或抛出 SQLException 异常,因不同的数据库提供者的 JDBC 驱动实现而异。

如果为当前行打开了一个输入流,对next()方法的调用将会隐式地关闭它。

当新的一行读入时,ResultSet对象的警告链将被清空。

 

这里需要注意的是

1. 开始指针位置位于第一行之前,也就是说,第一次调用next()方法后才能开始获取数据。这种做法的好处在于可以方便地配合while进行遍历,而不用担心取不到第一行。

while(re.next()){
    //re.getXXX()
}

2. 指针移动到最后一行之后返回false,可以用作循环结束的条件。在两种情况下:遍历到最后一行之后;或者当 ResultSet 中没有元素、行数为0时,返回false退出循环。

 

 

 

 

  • 18
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实验六: 给定一数据库test,数据库任选。给定表student,字段任意,实现对该表的增、删、改 、查操作。 实验步骤: 1.建立一个连接用户在访问数据库时要在JDBC管理层java.sql.DriverManager.getConn ection方法产生一个java.sql.Connection对象。该方法使用一个URL串作为地址参数. 2.选择合适的驱动程序。在数据库URL中,可以指定驱动程序的名称,也可不指定. 3.数据库URL在连接时,由数据库URL指定要连接的数据库. 4.查询:执行一条查询语句后,返回的是由java.sql.ResultSet对象访问的行的集合。在 该对象中提供了一系列get方法,访问当前的每一个数据项,ResultSet.next方法可实现 在结果集的记录之间移动,可以使用数据项。 5.输入参数(IN):Java.sql.PrepareStatement接口提供了一系列setXXX方法向SQL语句 传递输入参数,实现动态的SQL语句。在传递参数时,必须满足数据类型一致的要求. 6.输出参数(OUT):在调用一个存储过程时,可用setXXX方法传递输入参数,使用输出参 数接收返回结果。在使用时,必须先调用CallableStatement.registerOutParameter方 法为每一个输出参数进行类型注册,然后执行该过程调用语句,最后使用getXXX方法取 出输出参数的结果. 7.SQL数据类型到Java类型的转换:SQL数据类型与Java类型的差异较大,JDBC提供了详细 的SQL类型与Java类型转换的方法. 实验步骤: 1.加载驱动连接数据库或设置系统的ODBC数据源后建立数据库的连接 2.发送访问、操作数据库的SQL语句,加载JDBC- ODBC桥驱动程序用class.forName方法来完成 3.处理对数据库访问的结果import java.sql.*; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; public class jdbc { String drivename="com.mysql.jdbc.Driver"; String url="jdbc:mysql://10.1.26.224:3306/test"; String user="root"; String phonenumber="123456"; String insql; String upsql; String delsql; String sql="select * from user"; String name; Connection conn; ResultSet rs=null; public Connection ConnectMysql() { try{ Class.forName(drivename); conn = (Connection) DriverManager.getConnection(url, user, phonenumber); if (!conn.isClosed()) { System.out.println("Succeeded connecting to the Database!"); } else { System.out.println("Falled connecting to the Database!"); } }catch(Exception ex){ ex.printStackTrace(); } return conn; } public void CutConnection(Connection conn) throws SQLException { try{ if(rs!=null); if(conn!=null);}catch(Exception e){ e.printStackTrace(); } finally{ rs.close(); conn.close(); } } class user { int userid; String username=""; String phonenumber=""; public void setId(int userid) { this.userid=userid; } public void setName(String username) { this.username=username; } public void setphonenumber(String phonenumber) { this.phonenumber=phonenumber; } public Integ

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值