15.12.2020

Prepared Statement Get Generated Keys Select

Prepared Statement Get Generated Keys Select Rating: 9,8/10 2841 reviews

Here is a small write-up which should help those who still write plain Java JDBC code. I know we have some wonderful persistence frameworks like Hibernate that make ones life comfortable but the reality is we still have to deal with plain old JDBC apis. If you are poor chap like me, below code should make your life easy.

An interface for a precompiled SQL Statement. An SQL Statement is put into a PreparedStatement and is precompiled so that it can be executed efficiently multiple times. Setter methods are supplied in the PreparedStatement interface for the setting of IN parameters for the statement. What are the types of JDBC Statements available? Write an example code for JDBC prepared statement. Write an example for JDBC prepared statement with ResultSet. How to get primary key value (auto-generated keys) from inserted queries using JDBC? Write a simple program for CallableStatement statement to execute stored procedure.

Prepared Statement Get Generated Keys Select 3

Problem statement:

Prepared

I just inserted a record in Oracle database using Java JDBC. The primary key column was auto populated by a sequence value. How should I get the last inserted records auto generated primary key?

Write an example for JDBC prepared statement with ResultSet. How to get primary key value (auto-generated keys) from inserted queries using JDBC? Write a simple program for CallableStatement statement to execute stored procedure. Write a program for CallableStatement statement with stored procedure returns OUT parameters. If you can't use RETURNGENERATEDKEYS, use another statement (a query this time) to do. SELECT lastinsertid Make sure that you definitely have an auto-increment primary key column in that table. In this example, while creating an instance of PreparedStatement, we have passed 2 arguments. 1st is the query itself and 2 nd is “Statement.RETURNGENERATEDKEYS“, which will help us to get the primary key value of the new row. The below code is used to provide parameters for Insert Query.

Prepared Statement Get Generated Keys Select 1

Solution:

The solution should be getGeneratedKeys(). This method was added in JDBC 3.0 and it should be used to get last auto generated key value.

See code snippet below:
Office 2013 pro product key generator.

The above code should give us auto generated primary key value. The one thing to note here is method prepareStatement(). We passed two arguments first the insert query string and second an array of column name. The column name should be the primary key column name of table where you inserting the record.

Check below source code to see complete solution.

Full solution

We have a database table called STUDENTS. We also have an oracle sequence called STUDENT_SEQ that we uses to generate primary key for STUDENTS table.

Prepared Statement Get Generated Keys Select 2016

In Java, we use plain JDBC calls to insert a record in STUDENTS table. We uses sequence STUDENT_SEQ to generate primary key. Once the record is inserted, we want the last inserted primary value.

Prepared Statement Get Generated Keys Select In Word

The above code is filled with comments and is pretty self explanatory. Finally we have last inserted value in studentId variable.

Prepared Statement Get Generated Keys Select 2

The getGeneratedKeys() method is key here. It gives us the result set of all auto generated key values. In our case as we have only one auto generated value (for student_id column) we get only single record in this result set.