Five methods for JDBC to read the Sequence Value of the newly inserted Oracle Database

Source: Internet
Author: User

Five methods for JDBC to read the Sequence Value of the newly inserted Oracle Database

Oracle's sequence implementation is very flexible, so it also brings about some usability issues. How to obtain the sequence value generated by the new inserted records is significantly different from other databases, this document introduces five methods for reading the Sequence Value of the newly inserted record.

Database scripts used for testing:

SQL> create table T1  2  (  3    ID NUMBER  4  ); Table createdSQL> create sequence SEQ_T1; Sequence created

// Public code: Get the public connection getconnection () throws exception {class. forname ("oracle. JDBC. driver. oracledriver "). newinstance (); connection conn = drivermanager. getconnection ("JDBC: oracle: thin: @ 127.0.0.1: 1521: dbname", "username", "password"); Return conn ;} // method 1 // use select seq_t1.nextval as ID from dual to obtain the new sequence value. // Then pass the latest value to the inserted statement through the variable: insert into T1 (ID) values (?) // Finally, return the sequence value. // The advantage of this method is that the code is simple and intuitive, and the majority of users are used. The disadvantage is that two SQL interactions are required and the performance is poor. Public int terminate () throws exception {connection conn = getconnection (); string vsql = "select seq_t1.nextval as ID from dual"; preparedstatement pstmt = (preparedstatement) Conn. preparestatement (vsql); resultset rs1_pstmt.exe cutequery (); RS. next (); int id = Rs. getint (1); RS. close (); pstmt. close (); vsql = "insert into T1 (ID) values (?) "; Pstmt = (preparedstatement) Conn. preparestatement (vsql); pstmt. setint (1, id1_1_pstmt.exe cuteupdate (); system. out. print ("ID:" + id); Return ID;} // method 2 // insert into T1 (ID) values (seq_t1.nextval) to insert data first. // Then use select seq_t1.currval as ID from dual to return the sequence value generated by the inserted record. // Note: seq_t1.currval indicates that the sequence value generated at the end of the current session is extracted. Because session isolation is used, you only need to ensure that the two SQL statements use the same connection, for connection pool applications, two SQL statements must be placed in the same transaction to ensure concurrency security. // If the session does not generate a sequence value, an error is returned using the seq_t1.currval syntax. // The advantage of this method can be returned after a record is inserted. This method is suitable for Business Code that cannot be modified due to data insertion. The disadvantage is that it requires two SQL interactions and poor performance, it is prone to concurrent security issues. Public int terminate () throws exception {connection conn = getconnection (); string vsql = "insert into T1 (ID) values (seq_t1.nextval)"; preparedstatement pstmt skip cuteupdate (); pstmt. close (); vsql = "select seq_t1.currval as ID from dual"; pstmt = (preparedstatement) Conn. preparestatement (vsql); resultset rs1_pstmt.exe cutequery (); RS. ne XT (); int id = Rs. getint (1); RS. close (); pstmt. close (); system. out. print ("ID:" + id); Return ID;} // method 3 // use the returning into syntax of PL/SQL, you can use the callablestatement object to set registeroutparameter to get the value of the output variable. // The advantage of this method is that as long as one SQL interaction is performed, the performance is good. The disadvantage is that PL/SQL syntax is used, and the code is not intuitive and rarely used. Public int insertdatareturnkeybyplsql () throws exception {connection conn = getconnection (); string vsql = "begin insert into T1 (ID) values (seq_t1.nextval) Returning ID into: 1; end ;"; callablestatement cstmt = (callablestatement) Conn. preparecall (vsql); cstmt. registeroutparameter (1, types.bigint);cstmt.exe cute (); int id = cstmt. getint (1); system. out. print ("ID:" + id); cstmt. close (); Return ID;} // Method 4 // use Prepa Redstatement's getgeneratedkeys method // Conn. the second parameter of preparestatement can be used to set the field Name List of generatedkeys. The variable type is a string array. // Note: preparestatement (vsql, statement) cannot be used in Oracle databases as in other databases. return_generated_keys) method. This syntax is used to retrieve the incremental data. // Oracle does not have an auto-increment type, but all uses sequence. If statement. return_generated_keys is passed, the rowid of the newly inserted record is returned, which is not the sequence value we need. // The advantage of this method is that the performance is good. As long as an SQL interaction is performed, the SQL statement is actually converted to the returning into syntax of Oracle internally. The disadvantage is that only Oracle10g is supported and less used. Public int terminate () throws exception {connection conn = getconnection (); string vsql = "insert into T1 (ID) values (seq_t1.nextval)"; preparedstatement pstmt = (preparedstatement) Conn. preparestatement (vsql, new string [] {"ID" specified parameter implements pstmt.exe cuteupdate (); resultset rs = pstmt. getgeneratedkeys (); RS. next (); int id = Rs. getint (1); RS. close (); pstmt. close (); system. out. print ("ID:" + id); Return ID;} // Method 5 // similar to method 3, the output parameter is set using the returning into syntax unique to Oracle, but the difference is that the oraclepreparedstatement object is used, the standard preparedstatement object in the JDBC specification cannot set output type parameters. // Finally, use getreturnresultset to get the newly inserted Sequence Value. // This method has the best performance because Oracle9i supports only one SQL interaction, the disadvantage is that only oraclepreparedstatement objects specific to Oracle JDBC can be used. Public int insertdatareturnkeybyreturninto () throws exception {connection conn = getconnection (); string vsql = "insert into T1 (ID) values (seq_t1.nextval) Returning ID into: 1 "; oraclepreparedstatement pstmt = (oraclepreparedstatement) Conn. preparestatement (vsql); pstmt. registerreturnparameter (1, types.bigint1_1_pstmt.exe cuteupdate (); resultset rs = pstmt. getreturnresultset (); RS. next (); int id = Rs. getint (1); RS. close (); pstmt. close (); system. out. print ("ID:" + id); Return ID ;}

The above five methods can implement functions. The following is a summary of the advantages and disadvantages of the five methods. For personal recommendation, the first method is required for general businesses, and the fifth method is used for businesses with high performance requirements.

Method

Introduction

Advantages

Disadvantages

Method 1

Use seq. nextval to retrieve the value and insert it as a variable.

The code is simple and intuitive, with the most users

Requires two SQL interactions, with poor performance

Method 2

Use seq. nextval to insert records directly, and then use seq. currval to retrieve the newly inserted values.

The sequence can be returned after a record is inserted, which is suitable for business code that is difficult to transform the business logic of data insertion.

Requires two SQL interactions, poor performance, and prone to concurrency security issues

Method 3

Use the returning into syntax of PL/SQL blocks, and use the callablestatement object to set the output parameter to get the newly inserted value.

Good performance when only one SQL interaction is performed

PL/SQL syntax is required, and the code is not intuitive and rarely used.

Method 4

To set preparedstatement, you need to return the field name of the new value, and then use getgeneratedkeys to get the newly inserted value.

Good performance, as long as one SQL Interaction

It is only supported by Oracle10g and is rarely used.

Method 5

Returning into syntax, use oraclepreparedstatement object to set output parameters, and use getreturnresultset to get new values

The best performance, because Oracle9i supports only one SQL interaction.

Only oraclepreparedstatement objects specific to Oracle JDBC can be used.

 
 

Author: ye Zhengsheng

Date: 2011-10-30

My Sina Weibo: http://weibo.com/yzsind

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.