Interview question
How do you retrieve auto-generated keys after an INSERT in JDBC? JDBC में INSERT के बाद auto-generated keys कैसे retrieve करें?
Answer
When inserting a row into a table with an auto-increment primary key, JDBC lets you retrieve that generated key immediately using RETURN_GENERATED_KEYS, avoiding a separate SELECT query.
String sql = 'INSERT INTO users (name, email) VALUES (?, ?)';
// Request generated keys when preparing the statement
PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, 'John');
pstmt.setString(2, 'john@example.com');
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
try (ResultSet generatedKeys = pstmt.getGeneratedKeys()) {
if (generatedKeys.next()) {
long newId = generatedKeys.getLong(1); // the auto-generated primary key
System.out.println('New user ID: ' + newId);
}
}
}
// Alternative: specifying which column names to retrieve (some drivers require this)
PreparedStatement pstmt2 = conn.prepareStatement(sql, new String[]{'id'});
// Common use case: inserting a parent row, then using its generated ID
// for a related child row insert within the same transaction
conn.setAutoCommit(false);
try {
long orderId;
PreparedStatement orderStmt = conn.prepareStatement(
'INSERT INTO orders (customer_id) VALUES (?)', Statement.RETURN_GENERATED_KEYS);
orderStmt.setInt(1, customerId);
orderStmt.executeUpdate();
try (ResultSet keys = orderStmt.getGeneratedKeys()) {
keys.next();
orderId = keys.getLong(1);
}
PreparedStatement itemStmt = conn.prepareStatement(
'INSERT INTO order_items (order_id, product) VALUES (?, ?)');
itemStmt.setLong(1, orderId);
itemStmt.setString(2, 'Widget');
itemStmt.executeUpdate();
conn.commit();
} catch (SQLException e) {
conn.rollback();
}Auto-increment primary key वाली table में row insert करते समय, JDBC RETURN_GENERATED_KEYS से generated key तुरंत retrieve करने देता है, अलग SELECT query से बचाता है।
String sql = 'INSERT INTO users (name, email) VALUES (?, ?)';
PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, 'John');
pstmt.setString(2, 'john@example.com');
int affectedRows = pstmt.executeUpdate();
if (affectedRows > 0) {
try (ResultSet generatedKeys = pstmt.getGeneratedKeys()) {
if (generatedKeys.next()) {
long newId = generatedKeys.getLong(1);
System.out.println('New user ID: ' + newId);
}
}
}
// Parent-child insert उदाहरण
conn.setAutoCommit(false);
try {
long orderId;
PreparedStatement orderStmt = conn.prepareStatement(
'INSERT INTO orders (customer_id) VALUES (?)', Statement.RETURN_GENERATED_KEYS);
orderStmt.setInt(1, customerId);
orderStmt.executeUpdate();
try (ResultSet keys = orderStmt.getGeneratedKeys()) {
keys.next();
orderId = keys.getLong(1);
}
PreparedStatement itemStmt = conn.prepareStatement(
'INSERT INTO order_items (order_id, product) VALUES (?, ?)');
itemStmt.setLong(1, orderId);
itemStmt.setString(2, 'Widget');
itemStmt.executeUpdate();
conn.commit();
} catch (SQLException e) {
conn.rollback();
}Was this answer clear?