mysql 如何设置自动增长序列 sequence(二) 在实际中调用

来源:互联网 发布:淘宝手机版联系客服 编辑:程序博客网 时间:2024/05/19 20:20

前面说了,如何创建自动增长序列,那么,如何应用到我们的项目中呢?


这里要具体地说明一下:


1、创建存储过程来为程序所用!

[sql] view plaincopyprint?
  1. DELIMITER //  
  2. create procedure pro_nextval(out userid int )  
  3. begin  
  4. declare u varchar(50);  
  5. set u='userid';  
  6. set userid=(select _nextval(u));  
  7. end  
  8. //  
  9.    

2、编写测试类:

[java] view plaincopyprint?
  1. package cn.shop.user.logic;  
  2.   
  3. import java.sql.CallableStatement;  
  4. import java.sql.Connection;  
  5. import java.sql.Types;  
  6.   
  7. import cn.shop.util.db.GetDBConnect;  
  8.      
  9. public class UserLogic {  
  10.   
  11.     public int getUerId() throws Exception {  
  12.         GetDBConnect db = new GetDBConnect();  
  13.         Connection con = db.getCon();  
  14.         String sql = "{call pro_nextval(?)}";  
  15.         CallableStatement cs = con.prepareCall(sql);  
  16.         cs.registerOutParameter(1, Types.INTEGER);   
  17.         cs.execute();  
  18.         int uid=0;  
  19.         uid = cs.getInt(1);  
  20.         System.out.println("***UID : "+uid+"!");  
  21.         return uid;  
  22.     }  
  23.   public static void main(String[] args) throws Exception {  
  24.     new UserLogic().getUerId();   
  25.    }  
  26. }  
  27.    


说明:具体的数据库操作都已经封装。关键的是测试方法:getUserId(); 这里用到了java调用存储过程的应用。


3、输出测试结果:


[html] view plaincopyprint?
  1. *****load db propreties file****  
  2. ******connection!******  
  3. ***UID : 122!  
  4.    

4、结束。

0 0
原创粉丝点击