JDBC returns an empty ResultSet (rs.isBeforeFirst() == true) although the table isn't empty

后端 未结 2 600
忘掉有多难
忘掉有多难 2021-01-18 08:19

I am trying to complete a DB access method for my Web Service. The service and the DB access methods work fine for all other tables in the DB, but this one particular method

相关标签:
2条回答
  • 2021-01-18 08:51

    isBeforeFirst() returns true if the next call to next() will put the cursor on the first row of the ResultSet. In other words, any successful query that has data, once executed, will produce a ResultSet with isBeforeFirst() returning true.

    Just remove that block from your code, and have the rs.next() loop deal with potentially empty ResultSets:

    try
    {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:lineappDB.db");
        c.setAutoCommit(false);
        System.out.println("Opened database successfully");
        stmt = c.createStatement();
        String query = String.format("SELECT * FROM VIDEOS");
    
        result = new Object[6];
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next())
        {
            result[0] = rs.getInt(1);
            result[1] = rs.getString(2);
            result[2] = rs.getString(3);
            result[3] = rs.getString(4);
            result[4] = rs.getString(5);
            result[5] = rs.getInt(6);
        }
    } 
    /* catch and finally snipped for clarity of the answer */
    
    0 讨论(0)
  • 2021-01-18 08:51

    Use if (!rs.isBeforeFirst()) instead of if (rs.isBeforeFirst()). your problem would be solved.

    0 讨论(0)
提交回复
热议问题