我写一个分页的程序,但是提示[Microsoft][ODBC SQL Server Driver][SQL Server]第 1 行: '@P1' 附近有语法错误。
实在看不出来哪里错了,部分代码贴出,请赐教。谢谢。
int count=0; //取得表的总记录数
Connection conn = DBConnection.getConn();
PreparedStatement ps = conn.prepareStatement("select count(*) from hotel where hstate=1 ");
ResultSet rs = ps.executeQuery();
if (rs.next()){
count = rs.getInt(1);
}else{
throw new SQLException();
}
rs.close();
ps.close();
//计算
totlepages = (count-1)/pagesize+1;
int n=curpage*pagesize;
int size = pagesize;
if (curpage == totlepages){ //最后一页
size = count + pagesize - n;
n = count;
}
//查询 ------------个人觉得应该是这里的SQL出现问题,我用ODBC驱动
ps=conn.prepareStatement("select top ? * from (select top ? * from hotel where hstate=1 order by hid) as x order by hid desc");
ps.setInt(1,size);
ps.setInt(2,n);
rs=ps.executeQuery() ;
while(rs.next() ){
dto.Hotel h=new dto.Hotel();
h.setHid(rs.getInt("hid"));//编号
h.set...........
hlist.add(h);
}
实在看不出来哪里错了,部分代码贴出,请赐教。谢谢。
int count=0; //取得表的总记录数
Connection conn = DBConnection.getConn();
PreparedStatement ps = conn.prepareStatement("select count(*) from hotel where hstate=1 ");
ResultSet rs = ps.executeQuery();
if (rs.next()){
count = rs.getInt(1);
}else{
throw new SQLException();
}
rs.close();
ps.close();
//计算
totlepages = (count-1)/pagesize+1;
int n=curpage*pagesize;
int size = pagesize;
if (curpage == totlepages){ //最后一页
size = count + pagesize - n;
n = count;
}
//查询 ------------个人觉得应该是这里的SQL出现问题,我用ODBC驱动
ps=conn.prepareStatement("select top ? * from (select top ? * from hotel where hstate=1 order by hid) as x order by hid desc");
ps.setInt(1,size);
ps.setInt(2,n);
rs=ps.executeQuery() ;
while(rs.next() ){
dto.Hotel h=new dto.Hotel();
h.setHid(rs.getInt("hid"));//编号
h.set...........
hlist.add(h);
}
ps.close();
----------------
rs和ps不是关闭了吗?
ps.close();我也曾经把他们注释掉,结果是一样的。不过我觉得应该要关闭吧,要不怕连接会冲突
ps=conn.prepareStatement("select top ? * from (select top ? * from hotel where hstate=1 order by hid) as x order by hid desc");
ps.setInt(1,size);
ps.setInt(2,n);
你的分页是通过sql数据库控制的,如果改为程序控制。放到集合里。