免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
JDBC讀取新插入Oracle數(shù)據(jù)庫Sequence值的5種方法

      Oracle的sequence實(shí)現(xiàn)非常靈活,所以也帶來一些易用性問題,如何取到新插入記錄生成的sequence值與其它數(shù)據(jù)庫有較大差別,本文詳國介紹了5種實(shí)現(xiàn)讀取新插入記錄sequence值的方法。

測試用的數(shù)據(jù)庫腳本:

  1. SQL> create table T1  
  2.   2  (  
  3.   3    ID NUMBER  
  4.   4  );  
  5.    
  6. Table created  
  7. SQL> create sequence SEQ_T1;  
  8.    
  9. Sequence created  

  1. //公共代碼:得到數(shù)據(jù)庫連接   
  2. public Connection getConnection() throws Exception{  
  3.     Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
  4.     Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:dbname""username""password");  
  5.     return conn;  
  6. }  
  7.   
  8. //方法一   
  9. //先用select seq_t1.nextval as id from dual 取到新的sequence值。   
  10. //然后將最新的值通過變量傳遞給插入的語句:insert into t1(id) values(?)    
  11. //最后返回開始取到的sequence值。   
  12. //這種方法的優(yōu)點(diǎn)代碼簡單直觀,使用的人也最多,缺點(diǎn)是需要兩次sql交互,性能不佳。   
  13. public int insertDataReturnKeyByGetNextVal() throws Exception {  
  14.     Connection conn = getConnection();  
  15.     String vsql = "select seq_t1.nextval as id from dual";  
  16.     PreparedStatement pstmt =(PreparedStatement)conn.prepareStatement(vsql);  
  17.     ResultSet rs=pstmt.executeQuery();  
  18.     rs.next();  
  19.     int id=rs.getInt(1);  
  20.     rs.close();  
  21.     pstmt.close();  
  22.     vsql="insert into t1(id) values(?)";  
  23.     pstmt =(PreparedStatement)conn.prepareStatement(vsql);  
  24.     pstmt.setInt(1, id);  
  25.     pstmt.executeUpdate();  
  26.     System.out.print("id:"+id);  
  27.     return id;  
  28. }  
  29.   
  30. //方法二   
  31. //先用insert into t1(id) values(seq_t1.nextval)插入數(shù)據(jù)。   
  32. //然后使用select seq_t1.currval as id from dual返回剛才插入的記錄生成的sequence值。   
  33. //注:seq_t1.currval表示取出當(dāng)前會(huì)話的最后生成的sequence值,由于是用會(huì)話隔離,只要保證兩個(gè)SQL使用同一個(gè)Connection即可,對(duì)于采用連接池應(yīng)用需要將兩個(gè)SQL放在同一個(gè)事務(wù)內(nèi)才可保證并發(fā)安全。   
  34. //另外如果會(huì)話沒有生成過sequence值,使用seq_t1.currval語法會(huì)報(bào)錯(cuò)。   
  35. //這種方法的優(yōu)點(diǎn)可以在插入記錄后返回sequence,適合于數(shù)據(jù)插入業(yè)務(wù)邏輯不好改造的業(yè)務(wù)代碼,缺點(diǎn)是需要兩次sql交互,性能不佳,并且容易產(chǎn)生并發(fā)安全問題。   
  36. public int insertDataReturnKeyByGetCurrVal() throws Exception {  
  37.     Connection conn = getConnection();  
  38.     String vsql = "insert into t1(id) values(seq_t1.nextval)";  
  39.     PreparedStatement pstmt =(PreparedStatement)conn.prepareStatement(vsql);  
  40.     pstmt.executeUpdate();  
  41.     pstmt.close();  
  42.     vsql="select seq_t1.currval as id from dual";  
  43.     pstmt =(PreparedStatement)conn.prepareStatement(vsql);  
  44.     ResultSet rs=pstmt.executeQuery();  
  45.     rs.next();  
  46.     int id=rs.getInt(1);  
  47.     rs.close();  
  48.     pstmt.close();  
  49.     System.out.print("id:"+id);  
  50.     return id;  
  51. }  
  52.   
  53. //方法三   
  54. //采用pl/sql的returning into語法,可以用CallableStatement對(duì)象設(shè)置registerOutParameter取得輸出變量的值。   
  55. //這種方法的優(yōu)點(diǎn)是只要一次sql交互,性能較好,缺點(diǎn)是需要采用pl/sql語法,代碼不直觀,使用較少。   
  56. public int insertDataReturnKeyByPlsql() throws Exception {  
  57.     Connection conn = getConnection();  
  58.     String vsql = "begin insert into t1(id) values(seq_t1.nextval) returning id into :1;end;";  
  59.     CallableStatement cstmt =(CallableStatement)conn.prepareCall ( vsql);   
  60.     cstmt.registerOutParameter(1, Types.BIGINT);  
  61.     cstmt.execute();  
  62.     int id=cstmt.getInt(1);  
  63.     System.out.print("id:"+id);  
  64.     cstmt.close();  
  65.     return id;  
  66. }  
  67.   
  68. //方法四   
  69. //采用PreparedStatement的getGeneratedKeys方法   
  70. //conn.prepareStatement的第二個(gè)參數(shù)可以設(shè)置GeneratedKeys的字段名列表,變量類型是一個(gè)字符串?dāng)?shù)組   
  71. //注:對(duì)Oracle數(shù)據(jù)庫這里不能像其它數(shù)據(jù)庫那樣用prepareStatement(vsql,Statement.RETURN_GENERATED_KEYS)方法,這種語法是用來取自增類型的數(shù)據(jù)。   
  72. //Oracle沒有自增類型,全部采用的是sequence實(shí)現(xiàn),如果傳Statement.RETURN_GENERATED_KEYS則返回的是新插入記錄的ROWID,并不是我們相要的sequence值。   
  73. //這種方法的優(yōu)點(diǎn)是性能良好,只要一次sql交互,實(shí)際上內(nèi)部也是將sql轉(zhuǎn)換成oracle的returning into的語法,缺點(diǎn)是只有Oracle10g才支持,使用較少。   
  74. public int insertDataReturnKeyByGeneratedKeys() throws Exception {  
  75.     Connection conn = getConnection();  
  76.     String vsql = "insert into t1(id) values(seq_t1.nextval)";  
  77.     PreparedStatement pstmt =(PreparedStatement)conn.prepareStatement(vsql,new String[]{"ID"});  
  78.     pstmt.executeUpdate();  
  79.     ResultSet rs=pstmt.getGeneratedKeys();  
  80.     rs.next();  
  81.     int id=rs.getInt(1);  
  82.     rs.close();  
  83.     pstmt.close();  
  84.     System.out.print("id:"+id);  
  85.     return id;  
  86. }  
  87.   
  88. //方法五   
  89. //和方法三類似,采用oracle特有的returning into語法,設(shè)置輸出參數(shù),但是不同的地方是采用OraclePreparedStatement對(duì)象,因?yàn)閖dbc規(guī)范里標(biāo)準(zhǔn)的PreparedStatement對(duì)象是不能設(shè)置輸出類型參數(shù)。   
  90. //最后用getReturnResultSet取到新插入的sequence值,   
  91. //這種方法的優(yōu)點(diǎn)是性能最好,因?yàn)橹灰淮蝧ql交互,oracle9i也支持,缺點(diǎn)是只能使用Oracle jdbc特有的OraclePreparedStatement對(duì)象。   
  92. public int insertDataReturnKeyByReturnInto() throws Exception {  
  93.     Connection conn = getConnection();  
  94.     String vsql = "insert into t1(id) values(seq_t1.nextval) returning id into :1";  
  95.     OraclePreparedStatement pstmt =(OraclePreparedStatement)conn.prepareStatement(vsql);  
  96.     pstmt.registerReturnParameter(1, Types.BIGINT);  
  97.     pstmt.executeUpdate();  
  98.     ResultSet rs=pstmt.getReturnResultSet();  
  99.     rs.next();  
  100.     int id=rs.getInt(1);  
  101.     rs.close();  
  102.     pstmt.close();  
  103.     System.out.print("id:"+id);  
  104.     return id;  
  105. }  

     以上5種方法都可以實(shí)現(xiàn)功能,以下是5種方法的優(yōu)缺點(diǎn)匯總,個(gè)人推薦性能要求一般的業(yè)務(wù)采用第一種方法,性能要求非常高業(yè)務(wù)采用第五種方法。

方法

簡介

優(yōu)點(diǎn)

缺點(diǎn)

方法一

先用seq.nextval取出值,然后用轉(zhuǎn)入變量的方式插入

代碼簡單直觀,使用的人也最多

需要兩次sql交互,性能不佳

方法二

先用seq.nextval直接插入記錄,再用seq.currval取出新插入的值

可以在插入記錄后返回sequence,適合于數(shù)據(jù)插入業(yè)務(wù)邏輯不好改造的業(yè)務(wù)代碼

需要兩次sql交互,性能不佳,并且容易產(chǎn)生并發(fā)安全問題

方法三

用pl/sql塊的returning into語法,用CallableStatement對(duì)象設(shè)置輸出參數(shù)取到新插入的值

只要一次sql交互,性能較好

需要采用pl/sql語法,代碼不直觀,使用較少

方法四

設(shè)置PreparedStatement需要返回新值的字段名,然后用getGeneratedKeys取得新插入的值

性能良好,只要一次sql交互

只有Oracle10g才支持,使用較少

方法五

returning into語法,用OraclePreparedStatement對(duì)象設(shè)置輸出參數(shù),再用getReturnResultSet取得新增入的值

性能最好,因?yàn)橹灰淮蝧ql交互,oracle9i也支持

只能使用Oracle jdbc特有的OraclePreparedStatement對(duì)象

 
 
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
面向程序員的數(shù)據(jù)庫訪問性能優(yōu)化法則
在Hibernate中使用oracle的sequence產(chǎn)生主鍵 - wind_bell ...
Oracle 修改SEQ的值
java連接Oracle數(shù)據(jù)庫的工具類
Oracle提高數(shù)據(jù)處理效率---對(duì)于大批量數(shù)據(jù)來說例如幾百萬條數(shù)據(jù)的處理
jsp+oracle分頁查詢正傳
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服