标题:新人求助 web分页mysql中sql语句出错
只看楼主
a1026438864
Rank: 1
来 自:武汉
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-11-1
结帖率:0
已结贴  问题点数:20 回复次数:4 
新人求助 web分页mysql中sql语句出错
//====================分页=============
            int pagesize = 3;//每页显示多少条记录
            int pagenow = 1;//希望显示第几页
            int rowcount = 0;//一共有多少条记录  查表的来
            int pagecount = 0;//一共有几页  计算的来
        
            //得到rowcount
            Class.forName("com.mysql.jdbc.Driver");
            cn = DriverManager.getConnection("jdbc:mysql://localhost/userlist", "root", "123");
            ps = cn.prepareStatement("select count(*) from userlist");
            rs = ps.executeQuery();
            
            if(rs.next()){
               
                rowcount = rs.getInt(1);
            }
            //计算pagecount
            if(rowcount%pagesize==0){
                pagecount = rowcount/pagesize;
            }else{
                pagecount = rowcount/pagesize+1;
            }
            String sql = "select * from userlist limit ("+pagenow+"-1)*"+pagesize+","+pagesize+"";
            //String sql = "select * from userlist limit 0,3";
            ps = cn.prepareStatement(sql);
            //ps.setInt(1, pagenow);
            //ps.setInt(2, pagesize);
            //ps.setInt(3, pagesize);
            
            rs = ps.executeQuery();
            
            pw.println("<table border=1>");
            pw.println("<tr><th>ID</th><th>NAME</th><th>PASSWD</th><th>EMAIL</th><th>GRADE</th></tr>");   
            while(rs.next()){
               
                pw.println("<tr>");
                pw.println("<td>"+rs.getInt(1)+"</td>");
                pw.println("<td>"+rs.getString(2)+"</td>");
                pw.println("<td>"+rs.getString(3)+"</td>");
                pw.println("<td>"+rs.getString(4)+"</td>");
                pw.println("<td>"+rs.getInt(5)+"</td>");
                pw.println("</tr>");
               
            }
            pw.println("</table>");


错误You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(1-1)*3,3' at line 1
搜索更多相关主题的帖子: localhost sql语句 mysql count 记录 
2013-11-01 20:59
ren829
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:15
帖 子:255
专家分:1174
注 册:2006-3-11
得分:20 
这是sql语法错误 select * from userlist limit ("+pagenow+"-1)*"+pagesize+","+pagesize+"";
select * from userlist limit +"("+pagenow+"-1)*"+pagesize+","+pagesize+"";
2013-11-04 15:49
落泪的鱼7
Rank: 1
等 级:新手上路
帖 子:12
专家分:7
注 册:2013-11-20
得分:0 
select * from userlist limit ("+pagenow+"-1)*"+pagesize+","+pagesize+"";

刚开始明显少了一个引号没看见。。。?
2013-11-21 22:25
落泪的鱼7
Rank: 1
等 级:新手上路
帖 子:12
专家分:7
注 册:2013-11-20
得分:0 
select * from userlist limit ("+pagenow+"-1)*"+pagesize+","+pagesize+"";

刚开始明显少了一个引号没看见。。。?
2013-11-21 22:25
qwe885167759
Rank: 4
等 级:业余侠客
威 望:5
帖 子:148
专家分:259
注 册:2013-3-12
得分:0 
楼主检查一下你的SQL语句。应可能是这个的错误

我本楚狂人,凤歌笑孔丘
2015-06-05 15:46



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-422907-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.091560 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved