前面说了,如何创建自动增长序列,那么,如何应用到我们的项目中呢?
这里要具体地说明一下:
1、创建存储过程来为程序所用!
- DELIMITER //
- create procedure pro_nextval(out userid int )
- begin
- declare u varchar(50);
- set u='userid';
- set userid=(select _nextval(u));
- end
- //
2、编写测试类:
- package cn.shop.user.logic;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.Types;
- import cn.shop.util.db.GetDBConnect;
- public class UserLogic {
- public int getUerId() throws Exception {
- GetDBConnect db = new GetDBConnect();
- Connection con = db.getCon();
- String sql = "{call pro_nextval(?)}";
- CallableStatement cs = con.prepareCall(sql);
- cs.registerOutParameter(1, Types.INTEGER);
- cs.execute();
- int uid=0;
- uid = cs.getInt(1);
- System.out.println("***UID : "+uid+"!");
- return uid;
- }
- public static void main(String[] args) throws Exception {
- new UserLogic().getUerId();
- }
- }
说明:具体的数据库操作都已经封装。关键的是测试方法:getUserId(); 这里用到了java调用存储过程的应用。
3、输出测试结果:
- *****load db propreties file****
- ******connection!******
- ***UID : 122!
4、结束。