Java调用SQL server存储过程问题,求解(帮顶有分)
存储过程:
create proc getInfo
@sip varchar(50),
@dip varchar(50),
@Zfsbs int output,
@Zfssjl int output
as
begin
select @Zfsbs=(sum(a.trigger_times)+sum(b.repeat)), @Zfssjl=sum(b.traffic) from Program1 a, Program2 b where a.sip=@sip or b.sip=@sip
end
GO
Java代码:
Connection conn = null;
CallableStatement proc = null;
ResultSet rs = null;
String query = " ";
String Zfsbs = " ";
String Zfssjl = " ";
query = "{call getInfo(?,?,?,?)} ";
proc = conn.prepareCall(query);
proc.setString(1, "192.168.0.1 ");
proc.setString(2, "192.168.0.2 ");
proc.registerOutParameter(3,java.sql.Types.INTEGER);
proc.registerOutParameter(4,java.sql.Types.INTEGER);
rs = proc.executeQuery();
if (rs.next()){
Zfsbs = (String.valueOf(proc.getInt(3)));
Zfssjl = (String.valueOf(proc.getInt(4)));
}
错误:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]No ResultSet set was produced.
17:03:08,513 ERROR [jsp] - Servlet.service() for servlet jsp threw exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
……………………
……………………
请问我该怎么解决这个问题呢?
------解决方案--------------------为什么非要将你要取得的数值弄成输出参数呢?
把select @Zfsbs=(sum(a.trigger_times)+sum(b.repeat)), @Zfssjl=sum(b.traffic) from Program1 a, Program2 b where a.sip=@sip or b.sip=@sip
这句话改成select (sum(a.trigger_times)+sum(b.repeat)) as Zfsbs, sum(b.traffic) as Zfssjl from Program1 a, Program2 b where a.sip=@sip or b.sip=@sip
然后取得内容的时候就用rs.getString( "Zfsbs ")和rs.getString( "Zfssjl ")直接取到字符串类型的,没必要费那么大力气去取得整型然后再转化成字符串吧?试试如何?
还有就是这句“a.sip=@sip or b.sip=@sip”都用同一个输入参数?那你的存储过程中的@dip不用了?
------解决方案--------------------那lz就弄它四个输出参数:
create proc getInfo
@sip varchar(50),
@tip varchar(50),
@out1 int out,
@out2 int out,
@out3 int out,
@out4 int out
as
begin
select @out1=(sum(a.trigger_times)+sum(b.repeat)),@out2=sum(b.traffic) from Program1 a, Program2 b where a.sip=@sip or b.sip=@sip;
select @out3=(sum(a.trigger_times)+sum(b.repeat)),@out4=sum(b.traffic) from Program1 a, Program2 b where a.tip=@tip or b.tip=@tip;
end