三种最有效的操作Oracle Sequence的方法

  1. //方法一
  2. //采用pl/sql的returning into语法,可以用CallableStatement对象设置registerOutParameter取得输出变量的值。  
  3. //这种方法的优点是只要一次sql交互,性能较好,缺点是需要采用pl/sql语法,代码不直观,使用较少。  
  4. public int insertDataReturnKeyByPlsql() throws Exception {  
  5.     Connection conn = getConnection();  
  6.     String vsql = "begin insert into t1(id) values(seq_t1.nextval) returning id into :1;end;";  
  7.     CallableStatement cstmt =(CallableStatement)conn.prepareCall ( vsql);   
  8.     cstmt.registerOutParameter(1, Types.BIGINT);  
  9.     cstmt.execute();  
  10.     int id=cstmt.getInt(1);  
  11.     System.out.print("id:"+id);  
  12.     cstmt.close();  
  13.     return id;  
  14. }  
  15.   
  16. //方法二 
  17. //采用PreparedStatement的getGeneratedKeys方法  
  18. //conn.prepareStatement的第二个参数可以设置GeneratedKeys的字段名列表,变量类型是一个字符串数组  
  19. //注:对Oracle数据库这里不能像其它数据库那样用prepareStatement(vsql,Statement.RETURN_GENERATED_KEYS)方法,这种语法是用来取自增类型的数据。  
  20. //Oracle没有自增类型,全部采用的是sequence实现,如果传Statement.RETURN_GENERATED_KEYS则返回的是新插入记录的ROWID,并不是我们相要的sequence值。  
  21. //这种方法的优点是性能良好,只要一次sql交互,实际上内部也是将sql转换成oracle的returning into的语法,缺点是只有Oracle10g才支持,使用较少。  
  22. public int insertDataReturnKeyByGeneratedKeys() throws Exception {  
  23.     Connection conn = getConnection();  
  24.     String vsql = "insert into t1(id) values(seq_t1.nextval)";  
  25.     PreparedStatement pstmt =(PreparedStatement)conn.prepareStatement(vsql,new String[]{"ID"});  
  26.     pstmt.executeUpdate();  
  27.     ResultSet rs=pstmt.getGeneratedKeys();  
  28.     rs.next();  
  29.     int id=rs.getInt(1);  
  30.     rs.close();  
  31.     pstmt.close();  
  32.     System.out.print("id:"+id);  
  33.     return id;  
  34. }  
  35.   
  36. //方法三 
  37. //和方法一类似,采用oracle特有的returning into语法,设置输出参数,但是不同的地方是采用OraclePreparedStatement对象,因为jdbc规范里标准的PreparedStatement对象是不能设置输出类型参数。  
  38. //最后用getReturnResultSet取到新插入的sequence值,  
  39. //这种方法的优点是性能最好,因为只要一次sql交互,oracle9i也支持,缺点是只能使用Oracle jdbc特有的OraclePreparedStatement对象。  
  40. public int insertDataReturnKeyByReturnInto() throws Exception {  
  41.     Connection conn = getConnection();  
  42.     String vsql = "insert into t1(id) values(seq_t1.nextval) returning id into :1";  
  43.     OraclePreparedStatement pstmt =(OraclePreparedStatement)conn.prepareStatement(vsql);  
  44.     pstmt.registerReturnParameter(1, Types.BIGINT);  
  45.     pstmt.executeUpdate();  
  46.     ResultSet rs=pstmt.getReturnResultSet();  
  47.     rs.next();  
  48.     int id=rs.getInt(1);  
  49.     rs.close();  
  50.     pstmt.close();  
  51.     System.out.print("id:"+id);  
  52.     return id;  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuoShunkui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值