- Related Questions & Answers
Import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Main. java at com.mysql.jdbc.Statement.getGeneratedKeys(Statement.java:308) java at com.mysql.jdbc.PreparedStatement.getGeneratedKeys(PreparedSta tement.java:538) java at com.ace.db.DbHelper.insertObject(DbHelper.java:351) If someone can tell me how to correct this, or if there is a fix for it somewhere. Thanks -alex. Can we use PRIMARY KEY( column1, column2) in MySQL to make pairs? MySQL queries to update date records with NULL values; Auto insert values into a MySQL table in a range? How to work with auto incrementing column in MySQL? How to handle indexes in JavaDB using JDBC program? How to drop a table from JavaDB using JDBC?
- Selected Reading
JDBCObject Oriented ProgrammingProgramming
![Mysql Jdbc Return_generated_keys Mysql Jdbc Return_generated_keys](/uploads/1/2/6/0/126049444/716491751.jpg)
If you insert records into a table which contains auto-incremented column, using a Statement or, PreparedStatement objects.
You can retrieve the values of that particular column, generated by them object using the getGeneratedKeys() method.
![Mysql jdbc url Mysql jdbc url](/uploads/1/2/6/0/126049444/314312706.png)
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)
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)
Following JDBC program inserts 3 records into the Sales table (created above) using Statement, retrieves and displays the auto-incremented values generated by it.