MySQL JDBC determines whether the query results are empty and how to get the number of query result rows

Source: Internet
Author: User

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. AResultSetCursor 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 to thenextmethod returnsfalseThe cursor is positioned after the last row. Any invocation of aResultSetMethod which requires a current row would result in aSQLExceptionBeing thrown. If The result set type isTYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation would returnfalseor throw anSQLExceptionOn 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

Related Article

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.