简体   繁体   中英

ResultSet is not null yet resultset.next() returns false

I'm trying to search tables based on a certain criteria. The SQL query returns the correct result(1 row) when executed in SQL Developer. In JDBC, resultset is not null yet resultset.next() returns false.

Code:

public static ArrayList<SearchRecord> searchRecords(Connection conn, int orderID) throws SQLException {

    System.out.println("Order_id: " + orderID); //The correct orderID is passed
    ArrayList<SearchRecord> searchResult = null;
    SearchRecord searchRecord = null;
    PreparedStatement search_pstm = null;
    ResultSet search_rs = null;

    if (conn != null) {
        // This query is successfully executed in SQL Developer and outputs a row
        String search_sql = "select d.PPS_NUM, a.BRC_ORD_ID, PROJECT_NUM, LICENSE_NUM, EXPIRY_DATE, FIRST_NAME, LAST_NAME, ADDRESS1, ADDRESS2, CITY, PROVINCE, POSTAL_CODE from VD_SHIP_AOR a, VD_BRC_ORD b, VD_CONSOL_VALID c, VD_SHIP d where a.brc_ord_id = b.brc_ord_id and b.CONSOL_VALID_ID= c.consol_valid_id and a.SHIP_ID = d.SHIP_ID and d.ORD_ID = b.ORD_ID and d.PPS_NUM = ?";
        search_pstm = conn.prepareStatement(search_sql);
        search_pstm.setInt(1, orderID);
        search_rs = search_pstm.executeQuery();
        searchResult = new ArrayList<SearchRecord>();

        if (search_rs != null) {
            System.out.println("Not null"); //Prints this
            System.out.println(search_rs.isClosed()); //Prints false
            System.out.println(search_rs.getRow()); //Prints 0

            while (search_rs.next()) { //Does not enter the while loop, why?
                System.out.println("Inside while rs.next");
                //store resultset's data into arraylist
            }
        }
    }
    return searchResult;
}

What I considered referring:

Similar Stack Overflow question

Check for emptyResultSet

Similar Java Forums question

But the existing solutions are not working for me, please help me with this.

You seem to assume that this method returns null if there are no rows, that is not the case. The method executeQuery() will never return null * . The API documentation specifically says:

Returns:
a ResultSet object that contains the data produced by the query; never null

So, your null -check is entirely unnecessary. It will either produce a result set (even if the query produced no rows), or it will throw an SQLException for example for syntax errors, or if the executed statement does not produce a result set (eg an update or delete statement).

A result set after execution is initially open, and positioned before the first row (if any). A call to ResultSet.next() returns false if there are no more rows. In this case it is plausible that your select statement did not produce any rows, so the result set is empty and the first call to next() will return false .

I think your assumptions about the API are incorrect. I suggest you read the JDBC API documentation carefully, and maybe read some tutorials or introductory texts on JDBC.

If SQL Developer shows a row and your code doesn't, carefully check if 1) you are really using the same database, 2) the id used is really the same, and 3) if the data you are viewing in SQL Developer is actually committed. And consider simplifying your query first, eg select only from one table, remove the specific condition on this order number so you select all rows, use the same literal value you use in SQL Developer (hardcoded in the query), etc. Also, are you are sure you're using the right condition, PPS_NUM doesn't look like an Order_id , while for example ORD_ID looks more like one.

* : ignoring the possibility of bugs in a specific driver implementation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM