偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

獲得自動增長的MySQL主鍵

數(shù)據(jù)庫 MySQL
如果需要獲得MySQL數(shù)據(jù)庫中自動增長的主鍵,應該如何實現(xiàn)呢?下文對該功能的實現(xiàn)方法作了詳細的介紹供您參考學習。

下面的腳本教您如何獲得自動增長的MySQL主鍵,如果您對MySQL主鍵方面感興趣的話,不妨一看,相信對您學習MySQL主鍵方面會有所啟迪。

  1. import java.sql.*;  
  2.  
  3. public class GetKey {  
  4.     ResultSet rs = null;  
  5.     Connection conn = null;  
  6.     Statement stmt = null;  
  7.  
  8.     // 加入同一個連接發(fā)生其他查詢,Key會被重寫所以不準確  
  9.     public void getId() {  
  10.         try {  
  11.             getConnect gc = new getConnect();  
  12.             conn = gc.getconn();  
  13.             Statement stmt = conn.createStatement();  
  14.             stmt.executeUpdate("insert into tb (name) values ('Key')");  
  15.             rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");  
  16.             int autoIncKeyFromFunc = -1;  
  17.             if (rs.next()) {  
  18.                 autoIncKeyFromFunc = rs.getInt(1);  
  19.                 System.out.println("autoIncKeyFromFunc: " + autoIncKeyFromFunc);  
  20.             }  
  21.         } catch (Exception e) {  
  22.             System.out.print("有異常發(fā)生!");  
  23.         }  
  24.     }  
  25.  
  26.     // getGeneratedKeys()是每次創(chuàng)建一個Statement 實例,所以是安全的!  
  27.     public void getId_() {  
  28.         try {  
  29.             getConnect gc = new getConnect();  
  30.             conn = gc.getconn();  
  31.             stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,  
  32.                     java.sql.ResultSet.CONCUR_UPDATABLE);  
  33.             stmt.executeUpdate("insert into tb (name) values ('x')");  
  34.             int autoIncKeyFromApi = -1;  
  35.             rs = stmt.getGeneratedKeys();  
  36.             if (rs.next()) {  
  37.                 autoIncKeyFromApi = rs.getInt(1);  
  38.                 System.out.println("Key returned from getGeneratedKeys():"  
  39.                         + autoIncKeyFromApi);  
  40.             }  
  41.         } catch (Exception e) {  
  42.             System.out.print("有異常發(fā)生!");  
  43.         }  
  44.     }  
  45.  
  46.     public static void main(String[] args) {  
  47.         GetKey get = new GetKey();  
  48.         get.getId();  
  49.         get.getId_();  
  50.     }  
  51. }  

 

 

 

【編輯推薦】

MySQL主鍵的設計原則

MySQL分區(qū)的優(yōu)點

Mysql分區(qū)表對函數(shù)的限制

定義MySQL事務的例子

創(chuàng)建MySQL存儲過程示例

責任編輯:段燃 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-08-20 10:18:32

DB2自動增長主鍵

2010-08-10 17:10:46

2010-08-03 13:23:47

DB2自動增長主鍵

2010-08-19 13:14:17

DB2自動增長主鍵

2018-05-29 15:17:00

NFV虛擬化網(wǎng)絡

2010-09-07 13:04:20

DB2數(shù)據(jù)庫

2019-10-21 08:08:34

MySQL數(shù)據(jù)庫主鍵

2009-07-16 11:21:19

ibatis主鍵自動生成

2009-03-09 08:43:02

智能手機移動OS

2009-06-26 16:21:22

Oracle自動增長Hibernate

2009-07-28 12:48:50

.net獲得當前插入行

2010-10-11 11:25:26

MySQL主鍵

2023-11-10 11:20:30

2010-11-22 10:29:42

MySQL字段自增長

2010-09-25 10:41:34

SQL SERVER主

2010-09-25 09:18:45

sql server主

2010-10-08 11:52:29

2010-05-14 14:30:07

安裝MySQL

2011-03-22 10:36:39

Oracle數(shù)據(jù)庫自動增長列

2011-08-22 16:56:07

MySQL自動增長列
點贊
收藏

51CTO技術(shù)棧公眾號