首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Java调用SQL server存储过程有关问题,求解(帮顶有分)

2011-12-06 
Java调用SQL server存储过程问题,求解(帮顶有分)存储过程:createprocgetInfo@sipvarchar(50),@dipvarchar(

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

热点排行