This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JDBC insert error  RSS feed

 
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!
 
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.
 
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.
 
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')"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!