
How to write code for jdbc insert that returns primary key?
Help me fast
Thanks

Hi,
Following code can be used:
//Add record into database
String queryInsert = "insert into keyworduniquetable (keyword) values (?)";
PreparedStatement pstmtInsert =
conn.prepareStatement(queryInsert,Statement.RETURN_GENERATED_KEYS);
pstmtInsert.setString(1, keyword);
pstmtInsert.executeUpdate();
ResultSet rsInsert = pstmtInsert.getGeneratedKeys();
if(rsInsert != null && rsInsert.next()){
int newId = rsInsert.getInt(1);
System.out.println("New id is: "+newId);
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.