日期:2014-05-18  浏览次数:20697 次

java调用mssql存储过程后,记录集的返回为null
存储过程:
create   proc   proc_pageList
(
        @tabName   varchar(100),
        @columnName   varchar(200),
        @idName   varchar(100),
        @pageSize   int,
        @pageIndex   int,
        @pageCounts   int   output,
        @rowCounts   int   output
)
as
select   @rowCounts   =   count(*)   from   buymessageinfo--@tabName
set   @pageCounts   =   Ceiling(@rowCounts   *1.0   /   @pageSize)
Declare   @sql   nvarchar(200)
if   @pageIndex   <   @pageCounts
        set   @sql   =   'Select   '+@columnName+ '   from   (select   top   '+   str(@pageSize)   + '   *   from   (select   top   '+   str(@pageIndex   *   @pageSize)   + '   *   from   buymessageinfo)   as   tab1   order   by   '+@idName+ '   desc)   as   tab2   order   by   '+@idName
else
        set   @sql   =   'Select   '+@columnName+ '   from   (select   top   '+   str(@rowCounts-(@pageIndex-1)*@pageSize)   + '   *   from   (select   *   from   buymessageinfo)   as   tab1   order   by   '+@idName+ '   desc)   as   ta2   order   by   '+@idName
exec   (@sql)


java:
public   static   ArrayList   execProcPageList(String   procSql,String   tableName,String   colunm,String   IdName,int   pageSize,   int   pageIndex)
{
ArrayList   al   =   new   ArrayList();
try   {
getConn();
cstmt   =   conn.prepareCall(procSql);
cstmt.setString(1,tableName);
cstmt.setString(2,colunm);
cstmt.setString(3,   IdName);
cstmt.setInt(4,   pageSize);
cstmt.setInt(5,pageIndex);
cstmt.registerOutParameter(6,   java.sql.Types.INTEGER);
cstmt.registerOutParameter(7,   java.sql.Types.INTEGER);
//cstmt.execute();
rs   =   cstmt.executeQuery();
rs   =   cstmt.getResultSet();
int   pageCounts   =   cstmt.getInt(6);
int   rowCounts   =   cstmt.getInt(7);
System.out.println(rs.next());
System.out.println(pageCounts);
System.out.println(rowCounts);
}   catch   (Exception   e)   {e.printStackTrace(); }
finally
{
try   {
if(!conn.isClosed())
{
cstmt.close();
rs.close();
conn.close();
}
}   catch   (SQLException   e)   {e.printStackTrace();}
}
return   al;

}

打印出来的结果:
false
4
14
0

为什么rs.next()这false的呢?基本上其它的方法都试过了。

------解决方案--------------------
你的方法用错了,首先要用registerOutParameter方法注册你要返回的参数,给你个例子吧
public String getSQL(String message_id){
String result = " ";
String sql = "{call dbo.usp_generate_filter