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