This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes How to insert autoincrement value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to insert autoincrement value?" Watch "How to insert autoincrement value?" New topic
Author

How to insert autoincrement value?

Marcin Kwiatkowski
Ranch Hand

Joined: Aug 06, 2007
Posts: 32

Hello, I've got a problem with this piece of code:



after I call this method SQLException is thrown: "Failed on insert row"
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Don't include the autoincrement column in your insert statement and it should work fine.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Marcin Kwiatkowski
Ranch Hand

Joined: Aug 06, 2007
Posts: 32

well... when i dont include first column i got this exception :/
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Count the number of '?' in your insert statement. Are you trying to define one for the autoincrement column? The number of parameters to be bound should be one less than the number of fields in the table (since the database will populate the autoincrement field, you don't have to).
Sylven Yip
Ranch Hand

Joined: Aug 30, 2007
Posts: 42
set the auto increment field to null
e.g.
suppose id is an auto increment field
insert into table(id,name) values(null,"aldsf");

if preparedstatement
insert into table(id,name) values(null,?);

pStmt.setString(1,"aldsf");
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to insert autoincrement value?