mysql 如何设置自动增长序列 sequence(二) 在实际中调用
2015-01-21 16:11
561 查看
前面说了,如何创建自动增长序列,那么,如何应用到我们的项目中呢?
这里要具体地说明一下:
1、创建存储过程来为程序所用!
[sql] view
plaincopyprint?
DELIMITER //
create procedure pro_nextval(out userid int )
begin
declare u varchar(50);
set u='userid';
set userid=(select _nextval(u));
end
//
2、编写测试类:
[java] view
plaincopyprint?
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、输出测试结果:
[html] view
plaincopyprint?
*****load db propreties file****
******connection!******
***UID : 122!
4、结束。
这里要具体地说明一下:
1、创建存储过程来为程序所用!
[sql] view
plaincopyprint?
DELIMITER //
create procedure pro_nextval(out userid int )
begin
declare u varchar(50);
set u='userid';
set userid=(select _nextval(u));
end
//
2、编写测试类:
[java] view
plaincopyprint?
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、输出测试结果:
[html] view
plaincopyprint?
*****load db propreties file****
******connection!******
***UID : 122!
4、结束。
相关文章推荐
- mysql 如何设置自动增长序列 sequence(二) 在实际中调用
- mysql 如何设置自动增长序列 sequence(一)
- [转]MySQL如何设置自动增长序列 SEQUENCE
- mysql 如何设置自动增长序列 sequence(一)
- mysql 如何设置自动增长序列 sequence(一)
- mysql 如何设置自动增长序列 sequence(一)
- MySQL如何设置自动增长序列 SEQUENCE
- MySQL如何设置自动增长列
- oracle如何设置序列自动增长
- MySQL如何创建用户表并将其ID设置为自动增长列?
- Mysql,SqlServer,Oracle主键自动增长的设置
- Oracle主键ID设置自动增长(序列+触发器)
- mysql,sql server,Oracle主键自动增长的设置
- CachedRowSet如何设置sql server中自动增长列。
- 如何在oracle中设置字段自动增长
- Mysql,SqlServer,Oracle主键自动增长的设置
- Mysql,SqlServer,Oracle主键自动增长的设置
- Mysql,SqlServer,Oracle,sqlite 主键自动增长的设置
- Mysql,SqlServer,Oracle主键自动增长的设置
- PowerDesigner设置MYSQL主键自动增长的方法