0% found this document useful (0 votes)
48 views4 pages

7.4 Retrieving Column Values Through JDBC: Auto - Increment

The document discusses three ways to retrieve AUTO_INCREMENT column values through JDBC in MySQL: 1) Using Statement.getGeneratedKeys() to retrieve the value after an INSERT statement; 2) Using a SELECT LAST_INSERT_ID() query; 3) Retrieving the value from an updatable ResultSet after calling insertRow(). Code examples are provided for each approach.

Uploaded by

sandeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views4 pages

7.4 Retrieving Column Values Through JDBC: Auto - Increment

The document discusses three ways to retrieve AUTO_INCREMENT column values through JDBC in MySQL: 1) Using Statement.getGeneratedKeys() to retrieve the value after an INSERT statement; 2) Using a SELECT LAST_INSERT_ID() query; 3) Retrieving the value from an updatable ResultSet after calling insertRow(). Code examples are provided for each approach.

Uploaded by

sandeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

7.

4 Retrieving AUTO_INCREMENT Column Values
through JDBC
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()
Statement stmt = null;
ResultSet rs = null;

try {

//
// Create a Statement instance that we can use for
// 'normal' result sets assuming you have a
// Connection 'conn' to a MySQL database already
// available

stmt = conn.createStatement();

//
// Issue the DDL queries for the table for this example
//

stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial");


stmt.executeUpdate(
"CREATE TABLE autoIncTutorial ("
+ "priKey INT NOT NULL AUTO_INCREMENT, "
+ "dataField VARCHAR(64), PRIMARY KEY (priKey))");

//
// Insert one row that will generate an AUTO INCREMENT
// key in the 'priKey' field
//

stmt.executeUpdate(
"INSERT INTO autoIncTutorial (dataField) "
+ "values ('Can I Get the Auto Increment Field?')",
Statement.RETURN_GENERATED_KEYS);

//
// Example of using Statement.getGeneratedKeys()
// to retrieve the value of an auto-increment
// value
//

int autoIncKeyFromApi = -1;

rs = stmt.getGeneratedKeys();

if (rs.next()) {
autoIncKeyFromApi = rs.getInt(1);
} else {

// throw an exception from here


}
System.out.println("Key returned from getGeneratedKeys():"
+ autoIncKeyFromApi);
} finally {

if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}

if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
}
Example 7.9 Connector/J: Retrieving AUTO_INCREMENT column values using SELECT
LAST_INSERT_ID()
Statement stmt = null;
ResultSet rs = null;

try {

//
// Create a Statement instance that we can use for
// 'normal' result sets.

stmt = conn.createStatement();

//
// Issue the DDL queries for the table for this example
//

stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial");


stmt.executeUpdate(
"CREATE TABLE autoIncTutorial ("
+ "priKey INT NOT NULL AUTO_INCREMENT, "
+ "dataField VARCHAR(64), PRIMARY KEY (priKey))");

//
// Insert one row that will generate an AUTO INCREMENT
// key in the 'priKey' field
//

stmt.executeUpdate(
"INSERT INTO autoIncTutorial (dataField) "
+ "values ('Can I Get the Auto Increment Field?')");

//
// Use the MySQL LAST_INSERT_ID()
// function to do the same thing as getGeneratedKeys()
//

int autoIncKeyFromFunc = -1;


rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");

if (rs.next()) {
autoIncKeyFromFunc = rs.getInt(1);
} else {
// throw an exception from here
}

System.out.println("Key returned from " +


"'SELECT LAST_INSERT_ID()': " +
autoIncKeyFromFunc);

} finally {

if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}

if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
}
Example 7.10 Connector/J: Retrieving AUTO_INCREMENT column values in Updatable
ResultSets
Statement stmt = null;
ResultSet rs = null;

try {

//
// Create a Statement instance that we can use for
// 'normal' result sets as well as an 'updatable'
// one, assuming you have a Connection 'conn' to
// a MySQL database already available
//

stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
java.sql.ResultSet.CONCUR_UPDATABLE);

//
// Issue the DDL queries for the table for this example
//

stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial");


stmt.executeUpdate(
"CREATE TABLE autoIncTutorial ("
+ "priKey INT NOT NULL AUTO_INCREMENT, "
+ "dataField VARCHAR(64), PRIMARY KEY (priKey))");

//
// Example of retrieving an AUTO INCREMENT key
// from an updatable result set
//

rs = stmt.executeQuery("SELECT priKey, dataField "


+ "FROM autoIncTutorial");

rs.moveToInsertRow();
rs.updateString("dataField", "AUTO INCREMENT here?");
rs.insertRow();

//
// the driver adds rows at the end
//

rs.last();

//
// We should now be on the row we just inserted
//

int autoIncKeyFromRS = rs.getInt("priKey");

System.out.println("Key returned for inserted row: "


+ autoIncKeyFromRS);

} finally {

if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}

if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore

You might also like