Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get auto incremenet id while using java in mysql

 
Priya chek
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Iam facing a problem when Iam using java in mysql database. I have a auto -incremented field in my table named it as user_id. That auto -incremented userid is not generating while creating a new user in my table. Iam getting error like 'one field is missing' .. since it is a auto-incrementd field then why should i need to add it again for each entry of a new user. Iam using preparestatement to execuet query. why it is not incrementing automatically?? please help me ...
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take care to TellTheDetails. Could you provide us with the SQL statement and the exact error message / stacktrace, please?
 
Priya chek
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replu..

I have a table NEWUSER withh 4 fields

user_id, user,email, phone,address,dob
Iam getting error like

java.sql.SQLException: Column count doesn't match value count at row 1

This is my function

public static int register(String user,String email,String phone,String address,String dob) throws SQLException{

Connection c1=Connect.getCon();

java.sql.PreparedStatement ps=c1.prepareStatement("insert into NEWUSER values(?,?,?,?,?)");

ps.setString(1,user);
ps.setString(2,email);
ps.setString(3,phone);
ps.setString(4,address);
ps.setString(5,dob);
int status=ps.executeUpdate();
return status;


}

I kow that error occurs becAuse value is not giving to the field user_id in in the table NEWUSER..what iam doing wrong..please helppp..
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should specify columns you're inserting into in your insert statement, ie.

insert into the_table(column1, column2, ...) values (?, ?, ...).

and you won't include the autogenerated column in the column list.

Since it is allowed to insert values into autogenerated column, the database expects you to provide it. If you don't want to provide all values, you need to tell the database which columns you are actually inserting.

See also our wiki page on column lists.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic