JDBC调用SQL Server存储过程时,出现了了一点问题,请大虾们帮忙,万分感谢。
各位大虾,大家好!
小鸟遇到了一个问题。先来看程序,再来描述问题。
一、第一种情况
1、存储过程如下:
--创建查询存储过程
use pubs
go
if exists(select * from sysobjects where name='proc_Select')
drop procedure proc_Select
go
create procedure proc_Select
@out int output
as
select @out = count(*) from employee
go
--查询存储过程测试
declare @out int
exec proc_Select @out output
print @out
go
二、第二种情况
--创建查询存储过程
use pubs
go
if exists(select * from sysobjects where name='proc_Select')
drop procedure proc_Select
go
create procedure proc_Select
@out int output
as
select * from employee
select @out = count(*) from employee
go
--查询存储过程测试
declare @out int
exec proc_Select @out output
print @out
go
三、当时使用一下Java程序操作存储过程的时候,输出参数确接受到了不同了值,先来看看Java程序。
import java.sql.*;
public class Test {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:test");
String procname = "{call proc_Select(?)}";
CallableStatement smt = con.prepareCall(procname);
smt.registerOutParameter(1, Types.INTEGER);
smt.execute();
int count = smt.getInt(1);
System.out.println(count);
smt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
四、
(1)当我用第三部分Java应用程序调用第一部分的存储过程的时候,输出参数的值为当前结果集的总行数,例如:结果集有43行,此时输出43。
(2)但是当我用第三部门Java应用程序调用第二部分的存储过程的时候,输出参数的值始终为0。
请大虾求解,第一、二部分的存储过程就多了一条select * from employee语句。小鸟觉得是因为多个结果集产生的问题,也只是猜测,请各位大虾多多帮忙,解决此问题。
谢谢了。