原生jdbc方式:
示例:
Statement stmt = = <span style="color: #0000ff;">try<span style="color: #000000;"> {
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Create a Statement instance that we can use for
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 'normal' result sets assuming you have a
</span><span style="color: #008000;">//</span><span style="color: #008000;"> Connection 'conn' to a MySQL database already
</span><span style="color: #008000;">//</span><span style="color: #008000;"> available</span>
<span style="color: #000000;">
stmt =<span style="color: #000000;"> conn.createStatement();
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Issue the DDL queries for the table for this example
</span><span style="color: #008000;">//
<span style="color: #000000;">
stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial"<span style="color: #000000;">);
stmt.executeUpdate(
"CREATE TABLE autoIncTutorial ("
- "priKey INT NOT NULL AUTO_INCREMENT,"
- "dataField VARCHAR(64),PRIMARY KEY (priKey))"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Insert one row that will generate an AUTO INCREMENT
</span><span style="color: #008000;">//</span><span style="color: #008000;"> key in the 'priKey' field
</span><span style="color: #008000;">//
<span style="color: #000000;">
stmt.executeUpdate(
"INSERT INTO autoIncTutorial (dataField) "
- "values ('Can I Get the Auto Increment Field?')"<span style="color: #000000;">,Statement.RETURN_GENERATED_KEYS);
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Example of using Statement.getGeneratedKeys()
</span><span style="color: #008000;">//</span><span style="color: #008000;"> to retrieve the value of an auto-increment
</span><span style="color: #008000;">//</span><span style="color: #008000;"> value
</span><span style="color: #008000;">//
<span style="color: #0000ff;">int autoIncKeyFromApi = -1<span style="color: #000000;">;
rs </span>=<span style="color: #000000;"> stmt.getGeneratedKeys();
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (rs.next()) {
autoIncKeyFromApi </span>= rs.getInt(1<span style="color: #000000;">);
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> throw an exception from here</span>
<span style="color: #000000;"> }
System.out.println(</span>"Key returned from getGeneratedKeys():"
+<span style="color: #000000;"> autoIncKeyFromApi);
} <span style="color: #0000ff;">finally<span style="color: #000000;"> {
</span><span style="color: #0000ff;">if</span> (rs != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
rs.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SQLException ex) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> ignore</span>
<span style="color: #000000;"> }
}
</span><span style="color: #0000ff;">if</span> (stmt != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
stmt.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (SQLException ex) {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> ignore</span>
<span style="color: #000000;"> }
}
}
也有使用
Statement stmt = = <span style="color: #0000ff;">try<span style="color: #000000;"> {
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Create a Statement instance that we can use for
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 'normal' result sets.</span>
<span style="color: #000000;">
stmt =<span style="color: #000000;"> conn.createStatement();
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Issue the DDL queries for the table for this example
</span><span style="color: #008000;">//
<span style="color: #000000;">
stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial"<span style="color: #000000;">);
stmt.executeUpdate(
"CREATE TABLE autoIncTutorial ("
- "priKey INT NOT NULL AUTO_INCREMENT,PRIMARY KEY (priKey))"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Insert one row that will generate an AUTO INCREMENT
</span><span style="color: #008000;">//</span><span style="color: #008000;"> key in the 'priKey' field
</span><span style="color: #008000;">//
<span style="color: #000000;">
stmt.executeUpdate(
"INSERT INTO autoIncTutorial (dataField) "
- "values ('Can I Get the Auto Increment Field?')"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span>
<span style="color: #008000;">//</span><span style="color: #008000;"> Use the MySQL LAST_INSERT_ID()
</span><span style="color: #008000;">//</span><span style="color: #008000;"> function to do the same thing as getGeneratedKeys()
</span><span style="color: #008000;">//
<span style="color: #0000ff;">int autoIncKeyFromFunc = -1<span style="color: #000000;">;
rs = stmt.executeQuery("SELECT LAST_INSERT_ID()"<span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (rs.next()) {
autoIncKeyFromFunc </span>= rs.getInt(1<span style="color: #000000;">);
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008000;">//</span><span style="color: #008000;"> throw an exception from here</span>