The JDBC Insert statement is inserted into the Oracle database and the primary key of the returned data is jdbcoracle.

Source: Internet
Author: User

The JDBC Insert statement is inserted into the Oracle database and the primary key of the returned data is jdbcoracle.

Table Structure:

create table test(    id varchar2(32) primary key,    name varchar2(32));
Import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; public class Test {/*** use Statement. RETURN_GENERATED_KEYS indicates that the primary key */public static void main (String [] args) {PreparedStatement pst = null; ResultSet rs = null; Connection conn = ConnectionManager is returned. getConnection (); String SQL = "insert into test (id, name) v Alues (?,?) "; Try {// String generatedColumns [] = {" ID "}; // obtain the specified ID pst = conn. prepareStatement (SQL, Statement. RETURN_GENERATED_KEYS); // specify the primary key pst generated. setString (1, "123x"); pst. setString (2, "test"); int ii = pst.exe cuteUpdate (); // retrieves the key rs = pst generated by the object. getGeneratedKeys (); if (rs. next () {System. out. println ("Data primary key:" + rs. getString (1);} ConnectionManager. closeAll (rs, pst, conn); // close resource} catch (SQLException e) {e. printStackTrace ();}}}
The following are JDBC auxiliary classes, not the focus of this Article
Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; public class ConnectionManager {public static final String DRIVER = "oracle. jdbc. driver. oracleDriver "; public static final String URL =" jdbc: oracle: thin: @ localhost: 1521/orcl "; public static final String USERNAME =" root "; public static final String PASSWORD = "Root";/*** register the database driver through the static code block */static {try {Class. forName (DRIVER);} catch (ClassNotFoundException e) {e. printStackTrace () ;}}/*** get Connection ** @ return */public static Connection getConnection () {Connection conn = null; try {conn = DriverManager. getConnection (URL, USERNAME, PASSWORD);} catch (SQLException e) {e. printStackTrace ();} return conn;}/*** disable ResultSet * @ param rs */publi C static void closeResultSet (ResultSet rs) {if (rs! = Null) {try {rs. close ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** close Statement * @ param st */public static void closeStatement (Statement st) {if (st! = Null) {try {st. close ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** close Connection * @ param conn */public static void closeConnection (Connection conn) {if (conn! = Null) {try {conn. close ();} catch (SQLException e) {e. printStackTrace () ;}}/ *** close all * @ param rs * @ param sta * @ param conn */public static void closeAll (ResultSet rs, Statement sta, connection conn) {closeConnection (conn); closeStatement (sta); closeResultSet (rs );}}

Console printing:
Data primary key: AAAT2zAANAAACalAAB
And our primary key is the ID value of 123x. What is the output of AAAT2zAANAAACalAAB? Use rs. getObject (1) to print it on the console, as shown below:
Data primary key: oracle. SQL. ROWID @ 452bb7e0

It is actually the ROWID field value of the test table.


To obtain the value of the specified ID field, use the following method:

String generatedColumns[] = { "ID" };pst = conn.prepareStatement(sql, generatedColumns);

Author: itmyhome

Source: http://blog.csdn.net/itmyhome1990/article/details/41600309



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.