在说明之前,先做一下准备工作.下面的说明主要针对SQL Server数据库,另外要保证jtds驱动程序已经测试通过.
-
调用带有输入和输出参数的存储过程
假设已经存在一个存储过程user_registry_stat,其代码如下:
CREATE PROCEDURE user_registry_stat(@stylebook int,@result int output )AS
BEGIN
/*不返回计数*/
SET NOCOUNT ON
DECLARE @total_amount int
SET @total_amount = (SELECT count(id) as amount from CP_USER)
IF(@total_amount>@stylebook)
BEGIN
@result = 1
END
ELSE
BEGIN
@result =2
END
JAVA执行代码如下:
public static void executeStoredProcedure(Connection con) {
try {
CallableStatement cstmt = con.prepareCall("{call user_registry_s(?,?)}");
cstmt.seInt(1, 50);
cstmt.execute();
System.out.println("RETURN STATUS: " + cstmt.getInt(2));
cstmt.close();
}catch (Exception e) {
e.printStackTrace();
}
}
2. 调用带有返回状态的存储过程
假设已经存在一个存储过程user_registry_stat,其代码如下:
CREATE PROCEDURE user_registry_stat(@stylebook int)AS
BEGIN
/*不返回计数*/
SET NOCOUNT ON
DECLARE @total_amount int
SET @total_amount = (SELECT count(id) as amount from CP_USER)
IF(@total_amount>@stylebook)
return 1
ELSE
return 2
java执行代码如下:
public static void executeStoredProcedure(Connection con) {
try {
CallableStatement cstmt = con.prepareCall("{? = call user_registry_stat(?)}");
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
cstmt.seInt(2, 50);
cstmt.execute();
System.out.println("RETURN STATUS: " + cstmt.getInt(1));
cstmt.close();
}catch (Exception e) {
e.printStackTrace();
}
}
参考: http://msdn2.microsoft.com/zh-cn/library/ms378371.aspx
posted on 2006-12-11 18:42
zhangxl 阅读(467)
评论(0) 编辑 收藏 所属分类:
JDBC