How to determine whether the query result is null and obtain the number of rows in the query result through JDBC of MySQL

Source: Internet
Author: User
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.ResultSetCursor is initially positioned before the first row; the first call to the methodnextMakes the first row the current row; the second call makes the second row the current row, and so on.

    When a call tonextMethod returnsfalse, The cursor is positioned after the last row. Any invocation ofResultSetMethod which requires a current row will result inSQLExceptionBeing thrown. If the result set type isTYPE_FORWARD_ONLY, It is vendor specified whether their JDBC driver implementation will returnfalseOr throwSQLExceptionOn a subsequent callnext.

    If an input stream is open for the current row, a call to the methodnextWill implicitly close it.ResultSetObject's warning chain is cleared when a new row is read.

    Returns:
    trueIf the new current row is valid; falseIf 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 ResultSetWill result in SQLExceptionThrown. However, if the query result is set to TYPE_FORWARD_ONLY, the next method may return false or throw SQLExceptionThe 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.