Если Вы используете JDBC 3.0, то можно получить значение PK, как только Вы вставили его.
Вот статья, которая говорит о как: https://www.ibm.com/developerworks/java/library/j-jdbcnew /
Statement stmt = conn.createStatement();
// Obtain the generated key that results from the query.
stmt.executeUpdate("INSERT INTO authors " +
"(first_name, last_name) " +
"VALUES ('George', 'Orwell')",
Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
if ( rs.next() ) {
// Retrieve the auto generated key(s).
int key = rs.getInt(1);
}
Предполагая, что ваш драйвер находится в пути,
String url = "jdbc:mysql://localhost/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection conn = DriverManager.getConnection (url, "username", "password");
Вы читали документацию?
https://dev.mysql.com/doc /connector-j/en/connector-j-reference-configuration-properties.html
Базовая строка подключения выглядит так:
jdbc:mysql://localhost:3306/dbname
Строка class.forName - "com.mysql.jdbc.Driver", которую вы можете найти (редактировать: теперь на той же странице).