mysql 获取自增id的值的方法

原生jdbc方式:Statement.getGeneratedKeys()示例:Statement stmt = null;ResultSet rs = null;try {//// Create a Statement instance that we can use for// normal result sets assuming you have a// Connection conn to a MySQL database already// ava

原生jdbc方式:

示例:

Statement stmt = = <span style="color: #0000ff;">try<span style="color: #000000;"> {

</span><span style="color: #008000;"&gt;//</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Create a Statement instance that we can use for
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 'normal' result sets assuming you have a
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Connection 'conn' to a MySQL database already
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; available</span>

<span style="color: #000000;">
stmt =<span style="color: #000000;"> conn.createStatement();

</span><span style="color: #008000;"&gt;//</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Issue the DDL queries for the table for this example
</span><span style="color: #008000;"&gt;//

<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;"&gt;//</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Insert one row that will generate an AUTO INCREMENT
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; key in the 'priKey' field
</span><span style="color: #008000;"&gt;//

<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;"&gt;//</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Example of using Statement.getGeneratedKeys()
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; to retrieve the value of an auto-increment
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; value
</span><span style="color: #008000;"&gt;//

<span style="color: #0000ff;">int autoIncKeyFromApi = -1<span style="color: #000000;">;

rs </span>=<span style="color: #000000;"&gt; stmt.getGeneratedKeys();

</span><span style="color: #0000ff;"&gt;if</span><span style="color: #000000;"&gt; (rs.next()) {
    autoIncKeyFromApi </span>= rs.getInt(1<span style="color: #000000;"&gt;);
} </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; {

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; throw an exception from here</span>

<span style="color: #000000;"> }

System.out.println(</span>"Key returned from getGeneratedKeys():"
    +<span style="color: #000000;"&gt; autoIncKeyFromApi);

} <span style="color: #0000ff;">finally<span style="color: #000000;"> {

</span><span style="color: #0000ff;"&gt;if</span> (rs != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) {
    </span><span style="color: #0000ff;"&gt;try</span><span style="color: #000000;"&gt; {
        rs.close();
    } </span><span style="color: #0000ff;"&gt;catch</span><span style="color: #000000;"&gt; (SQLException ex) {
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; ignore</span>

<span style="color: #000000;"> }
}

</span><span style="color: #0000ff;"&gt;if</span> (stmt != <span style="color: #0000ff;"&gt;null</span><span style="color: #000000;"&gt;) {
    </span><span style="color: #0000ff;"&gt;try</span><span style="color: #000000;"&gt; {
        stmt.close();
    } </span><span style="color: #0000ff;"&gt;catch</span><span style="color: #000000;"&gt; (SQLException ex) {
        </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; ignore</span>

<span style="color: #000000;"> }
}
}

也有使用

Statement stmt = = <span style="color: #0000ff;">try<span style="color: #000000;"> {

</span><span style="color: #008000;"&gt;//</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Create a Statement instance that we can use for
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; 'normal' result sets.</span>

<span style="color: #000000;">
stmt =<span style="color: #000000;"> conn.createStatement();

</span><span style="color: #008000;"&gt;//</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Issue the DDL queries for the table for this example
</span><span style="color: #008000;"&gt;//

<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;"&gt;//</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Insert one row that will generate an AUTO INCREMENT
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; key in the 'priKey' field
</span><span style="color: #008000;"&gt;//

<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;"&gt;//</span>
<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; Use the MySQL LAST_INSERT_ID()
</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; function to do the same thing as getGeneratedKeys()
</span><span style="color: #008000;"&gt;//

<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;"&gt;if</span><span style="color: #000000;"&gt; (rs.next()) {
    autoIncKeyFromFunc </span>= rs.getInt(1<span style="color: #000000;"&gt;);
} </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; {
    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; throw an exception from here</span>

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

【免责声明】本站内容转载自互联网,其发布内容言论不代表本站观点,如果其链接、内容的侵犯您的权益,烦请提交相关链接至邮箱xwei067@foxmail.com我们将及时予以处理。

Copygight © 2017-2023 https://www.0746zz.com All Rights Reserved.永州站长网

站长:xwei067#foxmail.com(请把#换成@)

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息