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 autogenerated key

 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using mysql database there already i have selected primary key as a auto increment and now i want to know
should i write any code for this , am using servlet and java server pages both so please help me. m waiting for the answer
if somone is not egetting my question then ask me again plzzz tell me
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should you write any code to get the autogenerated key? Well, naturally you should only write code you need. So if you don't need to find out what the autogenerated key was after you did an insert statement, then by all means don't write any code which tries to do that.

However let's suppose you do need to find it. Here's the code I use while accessing MySQL:



This returns a ResultSet which you should read from.
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello thank you i am confuse actually where to fix this line and i am using java class
so after inserting query you tell me the code and you have given me complete separate function so i cant keep this
code over there please get back to me on this and i am unable to write complete code here its showing abbreviations error
 
walter bakshi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually sir i have three tables one is user information, second is question table and third last is option table so
i have done all the things i want to get print the question with there maximum option which is entered by
user so what happening when i am giving one option with one question it is proper but when i am giving more than
one option with same question so it is printing question every time with different option i want one question with their
possible entered option i am confused there with for loop and how to fix that key generation is not an actual
issue i thought may be it will helpful but its not so sir please tell me
 
Rob Spoor
Sheriff
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Should you write any code to get the autogenerated key? Well, naturally you should only write code you need. So if you don't need to find out what the autogenerated key was after you did an insert statement, then by all means don't write any code which tries to do that.

However let's suppose you do need to find it. Here's the code I use while accessing MySQL:



This returns a ResultSet which you should read from.

There is a way to use JDBC without having to use any RDBMS specific calls. Statement has methods execute(String sql, int autoGeneratedKeys) and executeUpdate(String sql, int autoGeneratedKeys). If the second argument is Statement.RETURN_GENERATED_KEYS then you can use Statement's getGeneratedKeys() to retrieve a ResultSet that contains the keys generated in the statement.

For PreparedStatement there is a similar method in java.sql.Connection to create a PreparedStatement that will have the generated keys available.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic