To determine whether the query result is null, there is no way to use hasNext in JDBC to determine whether there is any next data, but we can use the next method instead. Official explanation of the next method: booleannext () throwsMovesthecursorforwardonerowfromitscurrentposition. AResultSetcursorisinitiallypos
To determine whether the query result is null, there is no way to use hasNext in JDBC to determine whether there is any next data, but we can use the next method instead. Official explanation of the next method: booleannext () throws Moves the cursor forward one row from its current position. A ResultSet cursor is initially pos
To determine whether the query result is null, there is no way to use hasNext in JDBC to determine whether there is any next data, but we can use the next method instead. Refer to the official explanation of the next method:
boolean next() throws
Moves the cursor forward one row from its current position.ResultSet
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 tonext
Method returnsfalse
, The cursor is positioned after the last row. Any invocation ofResultSet
Method which requires a current row will result inSQLException
Being thrown. If the result set type isTYPE_FORWARD_ONLY
, It is vendor specified whether their JDBC driver implementation will returnfalse
Or throwSQLException
On a subsequent callnext
.
If an input stream is open for the current row, a call to the methodnext
Will implicitly close it.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 or this method is called on a closed result set
-
Boolean next () throws SQLException moves the current row from the previous row to the next row. The current row of a ResultSet points to the first row before the query result. When next is called for the first time, 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 the next method is called to return false, the current row is directed to the query result of the last row. At this time, any
ResultSet
Will result in
SQLException
Thrown. However, if the query result is set to TYPE_FORWARD_ONLY, the next method may return false or throw
SQLException
The exception warning will be clear.
For the start and end of next, you can use the following figure to explain: 0-> 1-> 2-> 3-> 4-> 1, 2, 3 in the middle of 0, 4 is the query result ^.
Correct posture for determining whether the JDBC query result is null:
Statement statement = conn.createStatement();ResultSet res = statement.executeQuery(selectSql);if (!res.next()) { //res is null} else { // res is not null}
JDBC does not directly provide a method to obtain the total number of rows of the query results for our call. Therefore, we need to use indirect means to execute:
Method 1:
ResultSet res =... use some method to obtain the query result int nRow = 0; while (res. next () {++ nRow;} res. beforeFirst (); // other codes remain unchanged
Method 2:
ResultSet res =... get the query result res. last (); final int nRow = res. getRow (); res. beforeFirst (); // other codes remain unchanged