JDBC INSERT statement Inserts Oracle database return data primary key

Source: Internet
Author: User

Table structure:

CREATE TABLE test (    ID varchar2 (+) 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 {/** * uses Statement.return_generated_keys to specify the return generated primary key */public static void Main (string[] Ar        GS) {PreparedStatement PST = NULL;        ResultSet rs = null;        Connection conn = Connectionmanager.getconnection ();        String sql = "INSERT INTO Test (id,name) VALUES (?,?)";  try {//string generatedcolumns[] = {"ID"}; Get the specified id PST = conn.preparestatement (sql, Statement.return_generated_keys);            Specifies that the generated primary key pst.setstring (1, "123x") is returned;            Pst.setstring (2, "test");             int II = Pst.executeupdate ();            Retrieves the key generated by the object rs = Pst.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 (); }    }}
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 database driver via static code block */static{try {class.forname (DRIVER);        } catch (ClassNotFoundException e) {e.printstacktrace (); }}/** * gets Connection * * @return */public static Connection getconnection () {Connecti        On conn = null;        try {conn = drivermanager.getconnection (Url,username,password);        }catch (SQLException e) {e.printstacktrace ();    } return conn; }/** * Close resultset * @param RS */public 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 printed Aaat2zaanaaacalaab, using Rs.getobject (1) To print in the console, is displayed as follows:
Data primary key: [email protected]

is actually the rowID field value of the test table


If you want to get the specified ID field value, you can use the following method:

String generatedcolumns[] = {"ID"};p st = conn.preparestatement (sql, generatedcolumns);

Itmyhome

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



JDBC INSERT statement Inserts Oracle database return data primary key

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.