This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi all. First of all would like to state that i'm quite new to Java programming and to JDBC as well. I'v been trying to learn JDBC basics, and i'm having trouble with inserting rows into database. I have MS access db called "Prison", table called "Prisoners" with collumns like this: ISN(numeric value) Firstname(TEXT), Offense(TEXT). It seems that I have problem with executeUpdate() method, but I don't understand wheres the problem, SQL syntax seems fine, and the strange thing is when i open database in Ms access it actually shows inserted rows, but i still have SQLException in my program. Here is the code sample:
I understand that PreparedStatements is better alternative, but i'm still experimenting here. Heres the stack trace
Error opening db: java.sql.SQLException: General error
java.sql.SQLException: General error
At line 31 you're calling executeBatch(), but you've not added any batch queries using addBatch(), instead you're using executeUpdate() calls.
This mix of approaches is probably what's tripping you up. Either remove the executeBatch() call, or change the executeUpdate() calls to addBatch() calls.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
I think you will have auto commit set to true (the default i believe), so each of your
lines are inserting and comitting the row to the database.
Then you call execute batch, but you haven't created a batch for it to exectue. You don't need this line.
Joined: Jun 22, 2012
Thanks for replies. Well if I replace executeUpdate() with addBatch() methods, I still got same SQLException, except now it prints error in executeBatch(). As for transaction management, if I change to setAutoCommit(false) I still got same error. I guess I better try that other approach with preparedstatements.