File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes JDBC insert error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC insert error" Watch "JDBC insert error" New topic
Author

JDBC insert error

Waldemar Macijewski
Ranch Hand

Joined: Jun 22, 2012
Posts: 32
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

Joined: Mar 10, 2008
Posts: 1763
    
    7

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.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

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

Joined: Jun 22, 2012
Posts: 32
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

Joined: Jun 22, 2005
Posts: 86
>"INSERT into Prisoners VALUES (999, 'John X', 'Drug Delear')"
Try "INSERT into Prisoners (column1,column2,column3 ) VALUES (999, 'John X', 'Drug Delear')"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC insert error