0

I don't really know what is going on so I'll just post part of the code

    int index2 = 0;

    while(rs.next()){
        System.out.println(rs.getInt("ItemID") + ", " + rs.getString("SocSecNum") + ", " + rs.getInt("Score"));

        if(rs.getInt("ItemID") == ID && rs.getString("SocSecNum").equals(socSecNum)){
            alreadySet = true;
            System.out.println("Flag 1");
            caq.executeUpdate("UPDATE ProjectScore SET Score = " + userScoreField.getText() +
                    " WHERE (ItemID = " + ID + ") AND (SocSecNum = '" + socSecNum + "')");
        }

        index2++;
        System.out.println(index2);
    }

    System.out.println("Flag 2");

Looks like it would work yeah? Here's the output:

1, 640730-7419, 3

Flag 1

1

This would imply that the while-loop is stuck somehow but there is no additional output (index2). Also, the database is updated exactly as it should but the program doesn't progress from here and "Flag 2" is never written out. Any ideas?

Edit:

catch (SQLException e1) {
                    System.out.println(e1.getMessage());
                    System.out.println(e1.getCause());
                }

Gives

Operation not allowed after ResultSet closed

null

Edit 2:

Here's the code used to make it work

    PreparedStatement statement = caq.getConnection().prepareStatement("SELECT * FROM ProjectScore WHERE SocSecNum = '"
    + socSecNum + "'", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    rs  = statement.executeQuery();
4
  • 4
    rs.next() is probably throwing an exception. Catch it. Commented Jan 4, 2013 at 2:25
  • Putting in more info in the question but yep that's right Commented Jan 4, 2013 at 2:29
  • So,... you're updating the query while still going through its ResultSet? Solution: don't do that. Commented Jan 4, 2013 at 2:42
  • Mind explaining how I'm doing that? I don't really know how a ResultSet work. Commented Jan 4, 2013 at 2:47

1 Answer 1

1

can you try instead of caq.executeUpdate connection.prepareStatement().executeUpdate() I am assuming caq.executeUpdate will cause the previous returned ResultSet object to close.

Also there seems to be a way to set concurrency level in Statement ResultSet.CONCUR_UPDATABLE
http://docs.oracle.com/javase/6/docs/api/java/sql/Connection.html#prepareStatement(java.lang.String,int,int)

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

4 Comments

Hmm connection doesn't have a method called getSatement() however I tried rs = caq.getStatement().executeQuery("SELECT * FROM ProjectScore WHERE SocSecNum = '" + socSecNum + "'"); but it didn't work either. I think it might be worth mentioning that I used the same ResultSet previously. Although I made another one and still had the same result.
edited the answer. it is not getStatement() is prepareStatement(), let me know know if that works. Sorry for the dump answer at first :-)
basically the problem is you are sending an update query in the loop, which will close the resultset and throws exception
Thanks man it worked perfectly :D much appreciated! It's going to take a while before I understand exactly why but I'll try to read up on it.

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.