JDBC returns empty ResultSet (rs.isBeforeFirst () == true) even though the table is not empty

I am trying to complete a DB access method for my webservice. DB services and access methods work fine for all other tables in the DB, but this particular method is not there. When I query the DB, it ResultSet

always returns empty (value isBeforeFirst() == true

).

After many attempts, I have shortened my query to a simple SELECT * FROM VIDEOS

one to see if there is a problem with some difference between the data I entered and the data I am using in my query, but even this simple query to select all items in a table did not return any result.

This is the method I use to output information from the DB:

public static Object[] getVideo(String phonenum, String timeStamp)
{
    Connection c = null;
    Statement stmt = null;
    Object[] result = null;
    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");
        ResultSet rs = stmt.executeQuery(query);

        // If no data was found
        if (rs.isBeforeFirst())
        {
            rs.close();
            stmt.close();
            c.close();
            return null;
        } else
        {
            result = new Object[6];
            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);
            }
        }
        rs.close();
        stmt.close();
        c.close();
    } catch (Exception e)
    {
        try
        {
            if (!c.isClosed())
            {
                c.commit();
                c.close();
            }
        } catch (Exception ex)
        {
            System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
            return null;
        }
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        return null;
    }
    System.out.println(String.format("Succesfully pulled from DB - %s %s", result[1], result[2]));
    return result;
}

      

Any help would be much appreciated.

IMAGE NOTIFICATION: This method is part of a web service that pulls the path to a specific video from the DB for sending to the client. Videos are downloaded by clients and then stored in the file system, and their paths are stored in the database itself.

As soon as I see the DB running, I replace SELECT * FROM VIDEOS

with SELECT * FROM VIDEOS WHERE PHONENUM = '%s' AND DATETIME = '%s'", phonenum, timeStamp

so that the query will pull the exact item I want.

+3


source to share


1 answer


isBeforeFirst()

returns true

if the next call next()

places the cursor on the first line ResultSet

, In other words, any successful query that has data, after execution, will return ResultSet

with a isBeforeFirst()

return true

.

Just remove this block from your code and process the loop rs.next()

with potentially empty ResultSet

s:



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 */

      

+3


source







All Articles