7.4 Retrieving AUTO_INCREMENT
Column Values through JDBC
- Get Generated Keys To Return Primary Key Using Jdbc Tutorial
- Get Generated Keys To Return Primary Key Using Jdbc Software
![Return Return](/uploads/1/3/3/8/133849041/485803782.jpg)
getGeneratedKeys()
is the preferred method to use if you need to retrieve AUTO_INCREMENT
keys and through JDBC; this is illustrated in the first example below. The second example shows how you can retrieve the same value using a standard SELECT LAST_INSERT_ID()
query. The final example shows how updatable result sets can retrieve the AUTO_INCREMENT
value when using the insertRow()
method. Example 7.8 Connector/J: Retrieving
AUTO_INCREMENT
column values using Statement.getGeneratedKeys()
The following are Jave code examples for showing how to use getGeneratedKeys of the java.sql.Statement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples. With older JDBC drivers for MySQL, you could always use a MySQL-specific method on the Statement interface, or issue the query SELECT LASTINSERTID after issuing an INSERT to a table that had an AUTOINCREMENT key. Using the MySQL-specific method call isn't portable, and issuing a SELECT to get the AUTOINCREMENT key's value requires another.
Example 7.9 Connector/J: Retrieving
AUTO_INCREMENT
column values using SELECT LAST_INSERT_ID()
Cd keys generator. Example 7.10 Connector/J: Retrieving
AUTO_INCREMENT
column values in Updatable ResultSets
![Using Using](/uploads/1/3/3/8/133849041/726751833.jpg)
Get Generated Keys To Return Primary Key Using Jdbc Tutorial
Running the preceding example code should produce the following output: https://yellowvertical384.weebly.com/ableton-962-key-generator.html.
Get Generated Keys To Return Primary Key Using Jdbc Software
At times, it can be tricky to use the
SELECT LAST_INSERT_ID()
query, as that function's value is scoped to a connection. So, if some other query happens on the same connection, the value is overwritten. On the other hand, the getGeneratedKeys()
method is scoped by the Statement
instance, so it can be used even if other queries happen on the same connection, but not on the same Statement
instance.