[JDK] java.sql.ResultSet/Statement

1 java.sql.ResultSet

next()

JDK Document

[jdk1.5doc]

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语句是否返回了结果集。
  • 对于INSERTUPDATEDELETE不生成结果集的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);
}
posted @ 2020-10-29 09:35  千千寰宇  阅读(324)  评论(0编辑  收藏  举报