Please tell me how to return the id of the record that was inserted using the INSERT. As an option, you can make another request after insertion, but this is not very.

I am writing in Java.

  • Show how your insertion is organized, via SQL-statement or something else? - carapuz
  • one
    " this is not very " - and the world is completely "very" :-) Well, if you don’t want to explicitly choose last_insert_id, that is, getGeneratedKeys() . - PinkTux
  • @carapuz using PreparedStatement - Tsyklop

1 answer 1

 PreparedStatement ps = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS); stmt.executeUpdate(); ResultSet rs = stmt.getGeneratedKeys(); rs.next(); return rs.getInt(1); 
  • Boolean, whether to return id. Removed so as not to be embarrassed - carapuz
  • rs.getInt(1); why exactly 1 ? and not "id" ? - Tsyklop
  • Because it takes an int, not a string, as a parameter. Rather, you can also String, but it is the column index that is recommended. javaportal.ru/java/tutorial/tutorialJDBC/resultset.html - carapuz
  • Thank. helped. - Tsyklop