8
I am making a Java application in conjunction with a Mysql database and would like to know what would be the best command to return an auto database increment ID right after the record is inserted.
My application will work with multiple simultaneous access to the database and would like in a way that no errors occur of the return type a wrong ID that was entered by another user.
I’m using JDBC
and I get the connection back this way:
private static final String URL = "jdbc:mysql://localhost/exemplows";
private static final String USER = "XXXXXXXX";
private static final String SENHA = "XXXXXXX";
public static Connection obtemConexao() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block e.printStackTrace();
}
return DriverManager.getConnection(URL, USER, SENHA);
}
You are using
JDBC
"pure" or something else, likeJPA
/Hibernate
?– Bruno César
private Static final String URL = "jdbc:mysql://localhost/exemplows"; private Static final String USER = "XXXXXXXX"; private Static final String PASSWORD = "XXXXXXX"; public Static Connection obtaineConexao() throws Sqlexception { Try { Class.forName("com.mysql.jdbc.Driver"); } catch (Classnotfoundexception e) { // TODO Auto-generated catch block e. printStackTrace(); } Return Drivermanager.getConnection(URL, USER, PASSWORD); }
– Nataniel Soares Rodrigues
My connection is this
– Nataniel Soares Rodrigues
Okay, you’re probably using
PreparedStatement
so I already include an answer for you.– Bruno César
I found this command on the internet ppst.getGeneratedKeys(). getInt("idOrdemCompra"); but not yet tested, does it work without risk of a bug id wrong?
– Nataniel Soares Rodrigues
Yes, that’s kind of it. See if my answer helps you.
– Bruno César