4

I made an update to a table in access via Java code, and it doesn't work. But, when I print to the console the result of executeUpdate(), it shows me 1, but in the database, no change. Can you tell me where's the problem, please?

System.out.println("here");
PreparedStatement st = conn.prepareStatement(
    "UPDATE StocProduseCuFactura SET cantitate = " +
    "cantitate - ? WHERE nume = ? and um = 'buc'");
st.setInt(1, cant);
st.setString(2, p.getNume());
System.out.println(st.executeUpdate());
st.close();

I forgot to say, if i run that SQL code in access, it's working.

4
  • are values of cant and p.getNume() correct? Also you have = cantitate - ?. Is that minus intentional ? Commented Aug 30, 2010 at 7:18
  • Are you using transactions manually? Do you commit the transaction properly? Commented Aug 30, 2010 at 7:26
  • yes, they are correct. cantitate - ? is the quantity from the database minus quantity to be decreased from it Commented Aug 30, 2010 at 7:27
  • before that code, I just simply get the connection, via drivermanager, and after that, i'm closing it. It's working in other cases, like insert, select, delete, but in this case - UPDATE - it prints the number of updates done, but there is no difference in the database, when i check it Commented Aug 30, 2010 at 7:32

4 Answers 4

1

people, sorry for bothering you, but I realized it was my stupid mistake. There is another method after this sequence of code, that made that table right as it was before the update, that why I didn't see any changes.

Thank you very much.

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

Comments

0

Have you ensured the transaction is commited?

2 Comments

I don't use Transaction, I use just a Connection and a PreparedStatement, with executeUpdate(). But in other cases it's working, and in this case too, it shows me the number of updates done, but in the database, no difference.
Trying calling the commit() method on the Connection object after you close the statement. So instead of just st.close() do: st.close(); conn.commit();
0

As others have told, transaction is the area you might need to focus upon. Try setting the autoCommit to true and false explicitly and try the update query.

conn.setAutoCommit(true);

Moreover, check for locks on the database. Some other connection might have acquired a lock and waiting for it to be committed, but then in that case, your update query would have not gotten fired at all. But just try restarting the database.

Comments

0

Johnny Keys is probably on the right trail. Most likely your connection isn't set to commit the transaction automatically, so when you close it the transaction is rolled back and your change is removed.

You might try putting

conn.setAutocommit(true); 

at the top to see if it makes a difference. The other possibility is "cant" is always zero. In that case you will be setting cantitate = cantitate (i.e. no change), but update will still report every row the "where" clause was satisfied.

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.