Return Auto Generated Key Query Jdbc Mysql Java

  

In this tutorial, we use the MySQL Connector/J driver. It is the official JDBC driver for MySQL. The examples were created and tested on Ubuntu Linux. You might also want to check Java tutorial, PostgreSQL Java tutorial, Apache Derby tutorial, MySQL tutorial,. @niraj - instead of ps.RETURNGENERATEDKEYS we can write Statement.RETURNGENERATEDKEYS because it is static variable in the java.sql.Statement class. – AmitG Jan 8 '15 at 16:24 add a comment. ResultSet res = stmt.getGeneratedKeys; while (res.next) System.out.println('Generated key: ' + res.getInt(1)); Sample Java source code to retrieve auto increment key values in MYSQL using JDBC prepared statement.

  1. Return Auto Generated Key Query Jdbc Mysql Java Server
  2. Mysql Jdbc Driver
  3. Java And Mysql Database
  4. Mysql Java Driver

6.4 Retrieving AUTO_INCREMENT Column Values through JDBC

Before version 3.0 of the JDBC API, there was no standard way of retrieving key values from databases that supported auto increment or identity columns. With older JDBC drivers for MySQL, you could always use a MySQL-specific method on the Statement interface, or issue the query SELECT LAST_INSERT_ID() after issuing an INSERT to a table that had an AUTO_INCREMENT key. Using the MySQL-specific method call isn't portable, and issuing a SELECT to get the AUTO_INCREMENT key's value requires another round-trip to the database, which isn't as efficient as possible. The following code snippets demonstrate the three different ways to retrieve AUTO_INCREMENT values. First, we demonstrate the use of the new JDBC 3.0 method getGeneratedKeys() which is now the preferred method to use if you need to retrieve AUTO_INCREMENT keys and have access to JDBC 3.0. 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 6.8 Connector/J: Retrieving AUTO_INCREMENT column values using Statement.getGeneratedKeys()


Example 6.9 Connector/J: Retrieving AUTO_INCREMENT column values using SELECT LAST_INSERT_ID()


Example 6.10 Connector/J: Retrieving AUTO_INCREMENT column values in Updatable ResultSets


Running the preceding example code should produce the following output:

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.

Return Auto Generated Key Query Jdbc Mysql Java
  • Related Questions & Answers
  • Selected Reading

Return Auto Generated Key Query Jdbc Mysql Java Server

JDBCObject Oriented ProgrammingProgramming

If you insert records into a table which contains auto-incremented column, using a Statement or, PreparedStatement objects.

Windows 10 pro key generator torrent. You can retrieve the values of that particular column, generated by them object using the getGeneratedKeys() method.

Example

Let us create a table with name sales in MySQL database, with one of the columns as auto-incremented, using CREATE statement as shown below −

Retrieving auto-generated values (PreparedStatement object)

Mysql Jdbc Driver

Following JDBC program inserts 3 records into the Sales table (created above) using PreparedStatement, retrieves and displays the auto-incremented values generated by it.

Example

Output

Retrieving auto-generated values (Statement object)

Java And Mysql Database

Following JDBC program inserts 3 records into the Sales table (created above) using Statement, retrieves and displays the auto-incremented values generated by it.

Mysql Java Driver

Output