0

My code:

Class.forName("org.sqlite.JDBC");
Connection C = DriverManager.getConnection("jdbc:sqlite:test.db");

Statement S = C.createStatement();
S.execute("CREATE TABLE NUMBER (VALUE INT(1))");
S.close();

S = C.createStatement();
S.execute("UPDATE NUMBER SET VALUE = 0");
S.close();

Statement S = C.createStatement();
ResultSet Value = S.executeQuery("SELECT VALUE FROM NUMBER");

if(Value.next())
{

    int Result = Value.getInt("VALUE");
    System.out.println("Success");

}
else
{

    System.out.println("Failure");

}

S.close();

I would expect the result of this code would be "Success", because it should retrieve the integer value 0 from the column VALUE in the table NUMBER. However, it instead prints "Failure". I cannot figure out if this is because the table is actually empty or because it just can't get the data or what. Any help would be appreciated.

1 Answer 1

2

You are executing an UPDATE Statement, but in the order the code is presented, nothing is persisted in Table NUMBER yet, as the UPDATE statement is used to update existing records in a table.

Use an INSERT Statement instead, thus inserting data first, as only this way tuples get created in the table NUMBER. Afterwards, the ResultSet should not be empty anymore.

If you need to learn the concept as such, you could have a look at this W3Schools guide.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.