我做的是条件查询,然后再分页显示查询结果。
总是报错。我却一直找不到问题所在。貌似问题出现在presentPageResult这个函数。可是我却不知道怎么改。我用的是myeclipse加mysql数据库。麻烦高手帮我一下,不吝赐教,感激不尽package pack;
import java.sql.*;
import com.sun.rowset.*;
public class link1
{    int pageSize=10;                       //每页显示的记录数
     int pageAllCount=0;                    //分页后的总页数
     int showPage=1;                    //当前显示页
     StringBuffer presentPageResult;       //显示当前页内容
     CachedRowSetImpl rowSet;             //用于存储ResultSet对象
     float priceMin,priceMax;
     String 字段[]=new String[100]   ;
     int 字段个数=0;
     public   link1()
     {   presentPageResult=new StringBuffer();
        try{   Class.forName("com.mysql.jdbc.Driver");
           }
        catch(Exception e){}
     }
     public void setPriceMax(float n)
     {
         priceMax=n;
        
          presentPageResult=new StringBuffer();
     }
     public float getPriceMax()
     {return priceMax;}
     public void setPriceMin(float n)
     {
         priceMin=n;
          presentPageResult=new StringBuffer();  
     }
     public float getPriceMin()
     {return priceMin;}
     public void setPageSize(int size)
     {   pageSize=size;
        字段个数=0;
        String uri="jdbc:mysql://localhost/f";
        String user="root";              //用户
                 
        try{   Connection con=DriverManager.getConnection(uri,user,"");
              DatabaseMetaData metadata=con.getMetaData();
              ResultSet rs1=metadata.getColumns(null,null,"book_shopping",null);int k=0;
              while(rs1.next())
               { 字段个数++;
                 字段[k]=rs1.getString(4); //获取字段的名字
                 k++;
               }
             Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
             ResultSet rs=sql.executeQuery("select * from book_shopping where price<="+priceMax+" AND "+"price>="+priceMin);
             rowSet=new CachedRowSetImpl();   //创建行集对象
             rowSet.populate(rs);
             con.close();                   //关闭连接
             rowSet.last();
             int m=rowSet.getRow();     //总行数
             int n=pageSize;
             pageAllCount=((m%n)==0)?(m/n):(m/n+1);
            }
         catch(Exception exp){}
     }
     public int getPageSize()
     {   return pageSize;
     }
     public int getPageAllCount()
     {   return pageAllCount;
     }
     public void setShowPage(int n)
     {   showPage=n;
     }
     public int getShowPage()
     { return showPage;
     }
   
     public StringBuffer getPresentPageResult()
     {  
         if(showPage>pageAllCount)
           showPage=1;
        if(showPage<=0)
           showPage=pageAllCount;
        presentPageResult=show(showPage);<input type="submit" value="提交">
   </form>
    价钱在<jsp:getProperty name="look" property="priceMin"/>至
   <jsp:getProperty name="look" property="priceMax"/>之间的记录     
<br>共有 <jsp:getProperty name="look" property="pageAllCount"/> 页.
<br>每页最多显示<jsp:getProperty   name="look"   property="pageSize" />条记录。
<jsp:getProperty   name= "look"   property="presentPageResult"   />
<BR>当前显示第 <jsp:getProperty   name= "look"   property="showPage"   /> 页,
<BR>单击“前一页”或“下一页”按纽查看记录
<Table>
   <tr><td><FORM action="">
           <Input type=hidden name="showPage" value="<%=look.getShowPage()-1 %>" >
           <Input type=submit name="g" value="前一页">
           </FORM>
       </td>
       <td><FORM action="">
           <Input type=hidden name="showPage" value="<%=look.getShowPage()+1 %>" >
           <Input type=submit name="g" value="后一页">
           </Form>
       </td>
       <td> <FORM action="">
            输入页码:<Input type=text name="showPage" size=5 >
            <Input type=submit name="g" value="提交">
            </FORM>
       </td>
   </tr>
   </Table>
</BODY></HTML>

解决方案 »

  1.   

    mysql不是用limit这个来处理分页么
      

  2.   

    用limit的飘过~~~关键是limit太好用了。
      

  3.   

    mysql> SELECT * FROM table LIMIT 5,10;  // 检索记录行 6-15//为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 -1: 
    mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.//如果只给定一个参数,它表示返回最大的记录行数目: 
    mysql> SELECT * FROM table LIMIT 5;     //检索前 5 个记录行
      

  4.   

    还不是不很懂。我用的是navicat for mysql
      

  5.   

    Mysql 的分页语句, 使用 limit 子句:  FROM table limit fromIndex, length;
    fromIndex 从 0 开始, 包含 fromIndex 指向的记录, length 要查询的记录的数量.
      

  6.   

    private int page = 1; //显示的页码
        private int pageSize = 3; //每页显示的订单数
        private int pageCount = 0; //页面总数
        private long recordCount = 0; //查询的记录总数
    ResultSet rs,rs1;
    String sqlStr="";
    public int getPage() { //显示的页码
    return page;
        }
        public void setPage(int newpage) {
          System.out.print(newpage);
          page = newpage;
        }
        public int getPageSize() { //每页显示的图书数
          return pageSize;
        }
        public void setPageSize(int newpsize) {
          pageSize = newpsize;
        }
        public int getPageCount() { //页面总数
          return pageCount;
        }
        public void setPageCount(int newpcount) {
          pageCount = newpcount;
        }
        public long getRecordCount() {
          return recordCount;
        }
        public void setRecordCount(long newrcount) {
          recordCount = newrcount;
        }
    public String getGbk(String str) {
          try {
            return new String(str.getBytes("gbk"));
          }
          catch (Exception e) {
            return str;
          }
        }
    public boolean getNews(HttpServletRequest str) throws Exception{
    javax.servlet.http.HttpServletRequest request = str;
    HttpSession session = request.getSession();
    String id=request.getParameter("t_id");
    session.setAttribute("t_id",id);
    int t_id=Integer.parseInt(id);
    sqlStr = "select count(*) from news"; //取出记录数
          int rscount = pageSize;
          try {
          dbPool d1 = new dbPool();
          rs1=d1.executeQuery(sqlStr);   
          if (rs1.next())
          recordCount = rs1.getInt(1);
          rs1.close();
          }
          catch (SQLException e) {
          return false;
          }
         //设定有多少pageCount
          if (recordCount < 1)
          pageCount = 0;
          else
          pageCount = (int) (recordCount - 1) / pageSize + 1;
          //检查查看的页面数是否在范围内
          if (page < 1)
          page = 1;
          else if (page > pageCount)
          page = pageCount;
          rscount = (int) recordCount % pageSize; // 最后一页记录数
         if (page == 1) {
                 sqlStr ="select * from news where menuid="+t_id+" order by id desc limit "+pageSize;
         }else{
         String id1=(String)session.getAttribute("t_id");
         int idi=Integer.parseInt(id1);
         sqlStr="select * from news where menuid="+idi+" order by id desc limit "+(pageSize*page-pageSize)+","+pageSize;
         }
    try {
    dbPool db = new dbPool();
    //System.out.println("==========="+sqlStr);
    rs=db.executeQuery(sqlStr);
    List list = new ArrayList();
    while(rs.next()) {
    News n = new News();
    n.setId(rs.getInt("id"));
    n.setName(rs.getString("name"));
    n.setTitle(rs.getString("title"));
    n.setWriter(rs.getString("writer"));
    n.setReporter(rs.getString("reporter"));
    n.setFromer(rs.getString("fromer"));
    n.setAddtime(rs.getString("addtime"));
    n.setFile(rs.getString("file"));
    n.setEditer(rs.getString("editer"));
    list.add(n);
    }
    session.setAttribute("adminNews",list);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return true;
    }