Determine if the query result is empty there is no method Hasnext in JDBC to determine if there is a next data, but we can use the next method instead. See the official explanation of next method:
Boolean Next ()
Moves the cursor forward one row from its current position. AResultSet
Cursor is initially positioned before the first row; The first call to the methodnext
Makes the first row the current row; The second call makes the second row the current row, and so on.When a call to thenext
method returnsfalse
The cursor is positioned after the last row. Any invocation of aResultSet
Method which requires a current row would result in aSQLException
Being thrown. If The result set type isTYPE_FORWARD_ONLY
, it is vendor specified whether their JDBC driver implementation would returnfalse
or throw anSQLException
On a subsequent call tonext
.
if an input stream was 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 is no more rows
-
Thro WS:
-
sqlexception
-if a database access error occurs or This method was called on a close D result set
-
-
translate as follows:The Boolean next () throws SQLException moves the current line from the previous row to the next row. Onethe current line of ResultSet initially points to the first row before the query results. When you first call next, the current row will point to the first row of query results. The second call will point to the second row of query results, and so on. when calling the next method returns False, After the current row of the current line points to the last row of query results. At this time, any
resultset
request the current line of method calls will result in
sqlexception
Be thrown. But if the result of the query is set to type_forward_only, Next method at this time according to the implementation of different manufacturers, may return false also pits can throw
sqlexception
exception the warning will be clear.
The start and end of next can be explained in the following diagram: 0->1->2->3->4->0 1, 2, 3, 4 is the result of the query ^ ^ Start end
Correct posture to determine if the JDBC query result is empty:
Statement Statement = Conn.createstatement (); ResultSet res = statement.executequery (selectsql), if (!res.next ()) { //res is null} else { //RES are NOT NULL}
gets the number of rows for the query resultJDBC does not directly provide a way to get the total number of query results to call us, so we need to use indirect means to execute:
The first method:
ResultSet res = ... Use a method to get the result of the query int nrow = 0;while (Res.next ()) { ++nrow;} Res.beforefirst ();//other code does not change
The second method:
ResultSet res = ... Use some method to get query results res.last (); final int nrow = Res.getrow (); Res.beforefirst ();//other code unchanged
MySQL JDBC Determines whether the query results are empty and how to get the number of query result rows