Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC insert error

 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6985)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3109)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:287)
at prisondb.PrisonDB.main(PrisonDB.java:31)


Any help is greatly appreciated, thanks!
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on Jelle's answer:

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.
 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wei Dai
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>"INSERT into Prisoners VALUES (999, 'John X', 'Drug Delear')"
Try "INSERT into Prisoners (column1,column2,column3 ) VALUES (999, 'John X', 'Drug Delear')"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic