[JDK] java.sql.ResultSet/Statement
1 java.sql.ResultSet
next()
JDK Document
[jdk1.5doc]
![](https://img2020.cnblogs.com/blog/1173617/202010/1173617-20201029093303559-1339815918.png)
Moves the cursor down 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.
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.
Returns:
true if the new current row is valid; false if there are no more rows
Throws:
SQLException - if a database access error occurs
JDK Document 解读/翻译
-
将指针移动到当前位置的下一行。
-
ResultSet 指针的初始位置位于第一行之前;第一次调用next()方法将会把第一行设置为当前行;第二次调用next()方法指针移动到第二行,以此类推。
-
当对next()方法调用返回 false,说明此时指针位于最后一行之后。所有对 ResultSet 需要使用当前行的方法[注:如getString()、getInt()等等]的调用都将导致next()方法抛出 SQLException 异常。如果返回的 ResultSet 集合的类型被设置为 TYPE_FORWARD_ONLY ,会在随后对next()方法的调用中返回 false 或抛出 SQLException 异常,因不同的数据库提供者的 JDBC 驱动实现而异。
-
如果为当前行打开了一个输入流,对next()方法的调用将会隐式地关闭它。
-
当新的一行读入时,ResultSet对象的警告链将被清空。
-
返回值:
如果新的当前行有效,则为true;如果没有更多的行,则为false
- 异常:
如果出现数据库访问错误
2 Statement
boolean : execute(sql)
statement.execute(sql)
方法在Java中用于执行一个SQL语句,如果该SQL语句是一个INSERT语句,那么执行该方法后通常不会返回结果集。- 该方法返回的是一个布尔值,表示执行的SQL语句是否返回了结果集。
- 对于
INSERT
、UPDATE
或DELETE
等不生成结果集的SQL语句,该方法返回false
。
例如
try(Statement statement = connector.getConnection().createStatement()) {
boolean insertResult = statement.execute(insertSql);//insertResult = false 恒成立 (因为是 INSERT 语句,无返回结果)
} catch (SQLException exception) {
log.error("Fail to insert data to database table({}) cause that exception : {}!", targetTable, exception);
}
![QQ沟通交流群](https://blog-static.cnblogs.com/files/johnnyzen/cnblogs-qq-group-qrcode.gif?t=1679679148)
本文作者:
千千寰宇
本文链接: https://www.cnblogs.com/johnnyzen
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!
本文链接: https://www.cnblogs.com/johnnyzen
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!