• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to insert autoincrement value?

 
Marcin Kwiatkowski
Ranch Hand
Posts: 32
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't include the autoincrement column in your insert statement and it should work fine.
 
Marcin Kwiatkowski
Ranch Hand
Posts: 32
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well... when i dont include first column i got this exception :/
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic