java - How to insert empty row and get autoincrement id? -
i want add empty row table , autoincrement id in java. im not sure why im still getting userid=0. java class:
public class userprofiledao { private dbconnectionmanager connectionmanager; private static final string insert_user = "insert user_profile values(default)"; private int userid; public userprofiledao(dbconnectionmanager connectionmanager) { this.connectionmanager = connectionmanager; try (connection connection = connectionmanager.getconnection()) { try (preparedstatement preparedstatement = connection.preparestatement(insert_user)) { try (resultset generatedkey = preparedstatement.getgeneratedkeys()) { if (generatedkey.next()) { userid = generatedkey.getint(1); } } } } catch (sqlexception e) { throw new runtimeexception(e); } } public int getuserid() { return userid; } }
you can use: lastval() next statement in current session , value
Comments
Post a Comment