日期:2014-05-17  浏览次数:20910 次

sql语句看不懂,高手来解释下
在网上下的strut1.2的图书馆系统源码,在这里出错了,返回的结果集为空,看半天不懂
public ArrayList getBooksList(int count, int page, String sqlstr) {
  ArrayList list = new ArrayList();
  try {
  proc = conn.prepareCall("{call BooksPage (?,?,?,?)}");
  proc.setInt(1, page);
  proc.setInt(2, count);
  proc.setString(3, sqlstr);
  proc.registerOutParameter(4, Types.INTEGER);
  res = proc.executeQuery();
  while (res.next()) {
  BooksBean bsb = new BooksBean();
  bsb.setBooks_Amount(res.getInt("Books_Amount"));
  bsb.setBooks_Company(res.getString("Books_Company"));
  bsb.setBooks_Count(res.getInt("Books_Count"));
  bsb.setBooks_ID(res.getInt("Books_ID"));
  bsb.setBooks_Name(res.getString("Books_Name"));
  bsb.setBooks_Time(res.getString("Books_Time"));
  bsb.setBookType_ID(res.getInt("BookType_ID"));
  list.add(bsb);
  }
  pagecount = proc.getInt(4);
  } catch (SQLException ex) {
  System.out.println("服务器异常发生在 getBooksList()");
  ex.printStackTrace();
  }
  return list;
  }
请大家赐教。。。。

------解决方案--------------------
if exists (select 1 from sysobjects where id = object_id('total_count') and type = 'U') drop table total_count
---找到用户创建的表'total_count' ,如果存在了就删除掉,然后在创建一个 'total_count' ;
create table total_count(toll int)
execute( 'insert into total_count select count(*) from Books ' + @sqlstr)


---把查询出来的数据插入到'total_count' 表
select @total_count = toll from total_count

--查询出数据赋值给@total_count变量,然后把表'total_count'删除掉
drop table total_count
declare @page_in_show int
set @page_in_show=@total_count-(@curPage-1)*@perPageRecords
set @countpage=(@total_count+@perPageRecords -1)/@perPageRecords 
exec('select top '+@perPageRecords+' * from (select top '+@page_in_show+' * from Books '+@sqlstr+' order by Books_ID desc) as a order by Books_ID asc')



你可以在查询分析器中执行 call BooksPage (?,?,?,?) 

问号用你的实际输入代替 ,执行一下就知道结果了 ;
4个问号的值分别赋值给 @curPage int=1,
@perPageRecords int,
@sqlstr varchar(255),
@countpage int output
这4个变量 ;