前面写了rs=stmt.executeQuery("select *from bookinfo;");到这里
while(rs.next())//为什么这条while只循环了一次? 只显示第一条数据
        {
            out.println("<tr>");
            out.println("<td>"+rs.getString("title")+"</td>");
            out.println("<td>"+rs.getString("author")+"</td>");
            out.println("<td>"+rs.getString("bookconcern")+"</td>");
            out.println("<td>"+rs.getFloat("price")+"</td>");
            out.println("<td>"+rs.getDate("publish_date")+"</td>");
            out.println("</tr>");
        }
        out.println("</table></body></html>");要是我在循环里面再加一句rs.next() 则它会显示第二条数据,也不会再循环  求解?

解决方案 »

  1.   


    List<Bookinfo> books=new ArrayList<Bookinfo>();
    while(rs.next()){
        String title = rs.getString("title");
        String author= rs.getString("author");
        String bookconcern= rs.getString("bookconcern");
        String price= rs.getString("price");
        String publish_date= rs.getString("publish_date");
        Bookinfo book = new Bookinfo();
        book.setTitle(title);
        book.setAuthor(author);
        //下面一样
         books.add(book);
    }
    for(Bookinfo book : books){
        out.println("<tr>");
        out.println("<td>"+book.getTitle+"</td>");
        out.println("<td>"+book.getAuthor+"</td>");
        //下面一样
    }这样试试 看行不行,不行就Debug  调试调试咯~~~
      

  2.   

    private void printBookInfo(PrintWriter out,ResultSet rs)
                     throws SQLException
        {
            out.println("<html><head>");
            out.println("<title>图书信息</title>");
            out.println("</head><body>");
            out.println("<table border=1><caption>图书信息</caption>");
            out.println("<tr><th>书名</th><th>作者</th><th>出版社</th><th>价格</th><th>发行日期</th></tr>");
            while(rs.next())//为什么这条while只循环了一次?
            {
                out.println("<tr>");
                out.println("<td>"+rs.getString("title")+"</td>");
                out.println("<td>"+rs.getString("author")+"</td>");
                out.println("<td>"+rs.getString("bookconcern")+"</td>");
                out.println("<td>"+rs.getFloat("price")+"</td>");
                out.println("<td>"+rs.getDate("publish_date")+"</td>");
                out.println("</tr>");
            }
            out.println("</table></body></html>");
        }
    这是这个类的代码
    ?????
    我没有Bookinfo这个类,这个是数据表
      

  3.   

    private void printBookInfo(PrintWriter out,ResultSet rs)
      throws SQLException
      {
      out.println("<html><head>");
      out.println("<title>图书信息</title>");
      out.println("</head><body>");
      out.println("<table border=1><caption>图书信息</caption>");
      out.println("<tr><th>书名</th><th>作者</th><th>出版社</th><th>价格</th><th>发行日期</th></tr>");
      rs.beforeFirst()   ;
      while(rs.next())//为什么这条while只循环了一次?
      {
      out.println("<tr>");
      out.println("<td>"+rs.getString("title")+"</td>");
      out.println("<td>"+rs.getString("author")+"</td>");
      out.println("<td>"+rs.getString("bookconcern")+"</td>");
      out.println("<td>"+rs.getFloat("price")+"</td>");
      out.println("<td>"+rs.getDate("publish_date")+"</td>");
      out.println("</tr>");
      }
      out.println("</table></body></html>");
      }  rs.beforeFirst()   ;  //加这一句看看 ,是不是你在别的地方用了 rs了,没有复位到第一条啊  ?
      while(rs.next())//为什么这条while只循环了一次?
      

  4.   

      rs.beforeFirst()   ;  //加这一句看看 ,是不是你在别的地方用了 rs了,没有复位到第一条啊  ?
      while(rs.next())//为什么这条while只循环了一次?
      

  5.   

    rs=stmt.executeQuery("select *from bookinfo;");
    * 和 from之间不用加空格吗?
    bookinfo表里有几条记录?
    printBookInfo(PrintWriter out,ResultSet rs)传了两个参数,我看看你是怎么调用的!
      

  6.   


    package org.ch04.servlet;import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class ListServlet extends HttpServlet
    {
        private String url;
        private String user;
        private String password;
        
        public void init() throws ServletException
        {
            ServletContext sc=getServletContext();
            String driverClass=sc.getInitParameter("driverClass");
            url=sc.getInitParameter("url");
            user=sc.getInitParameter("user");
            password=sc.getInitParameter("password");
            try
            {
                Class.forName(driverClass);
                
            }
            catch(ClassNotFoundException ce)
            {
             throw new ServletException("加载数据库驱动失败!");
            }
        }
        
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                   throws ServletException,IOException
        {
            Connection conn=null;
            Statement stmt=null;
            ResultSet rs=null;
            req.setCharacterEncoding("gb2312");
            String condition=req.getParameter("cond");
            if(null==condition || condition.equals(""))
            {
                resp.sendRedirect("search.html");
                return;
            }
            resp.setContentType("text/html;charset=gb2312");
            PrintWriter out=resp.getWriter();
            try
            {
                conn=DriverManager.getConnection(url,user,password);
                stmt=conn.createStatement();
                if(condition.equals("all"))
                {
                    rs=stmt.executeQuery("select *from bookinfo;");
                    printBookInfo(out,rs);
                    out.close();
                }
                else if(condition.equals("precision"))
                {
                    String title=req.getParameter("title");
                    String author=req.getParameter("author");
                    String bookconcern=req.getParameter("bookconcern");
                    
                    if((null==title || title.equals("")) &&
                       (null==author || author.equals("")) &&
                       (null==bookconcern || bookconcern.equals("")))
                    {
                        resp.sendRedirect("search.html");
                        return;
                    }
                    
                    StringBuffer sb=new StringBuffer("select * from bookinfo where ");
                    boolean bFlag=false;
                    
                    if(!title.equals(""))
                    {
                        sb.append("title = "+"'"+title+"'");
                        bFlag=true;
                    }
                    if(!author.equals(""))
                    {
                        if(bFlag)
                            sb.append("and author = "+"'"+author+"'");
                        else
                        {
                           sb.append("author = "+"'"+author+"'");
                           bFlag=true;
                        }
                    }
                    if(!bookconcern.equals(""))
                    {
                        if(bFlag)
                            sb.append("and bookconcern = "+"'"+bookconcern+"'");
                        else
                            sb.append("bookconcern = "+"'"+bookconcern+"'");
                    }
                    rs=stmt.executeQuery(sb.toString());
                    printBookInfo(out,rs);
                    out.close();
                }
                else if(condition.equals("keyword"))
                {
                    String keyword=req.getParameter("keyword");
                    if(null==keyword || keyword.equals(""))
                    {
                        resp.sendRedirect("search.html");
                        return;
                    }
                    String strSQL="select * from bookinfo where title like '%"+keyword+"%'";
                    
                    rs=stmt.executeQuery(strSQL);
                    printBookInfo(out,rs);
                    out.close();
                }
                else
                {
                    resp.sendRedirect("search.html");
                    return;
                }
            }
            catch(SQLException se)
            {
                se.printStackTrace();
            }
            finally
            {
             closeResultSet(rs);
             closeStatement(stmt);
             closeConnection(conn);         
            }
        }
        
        public void doPost(HttpServletRequest req, HttpServletResponse resp)
                   throws ServletException,IOException
        {
            doGet(req,resp);
        }
        
        private void printBookInfo(PrintWriter out,ResultSet rs)
                     throws SQLException
        {
            out.println("<html><head>");
            out.println("<title>图书信息</title>");
            out.println("</head><body>");
            out.println("<table border=1><caption>图书信息</caption>");
            out.println("<tr><th>书名</th><th>作者</th><th>出版社</th><th>价格</th><th>发行日期</th></tr>");
            while(rs.next())//为什么这条while只循环了一次?
            {
                out.println("<tr>");
                out.println("<td>"+rs.getString("title")+"</td>");
                out.println("<td>"+rs.getString("author")+"</td>");
                out.println("<td>"+rs.getString("bookconcern")+"</td>");
                out.println("<td>"+rs.getFloat("price")+"</td>");
                out.println("<td>"+rs.getDate("publish_date")+"</td>");
                out.println("</tr>");
            }
            out.println("</table></body></html>");
        }
        
        private void closeResultSet(ResultSet rs)
        {
         if(rs!=null)
            {
                try
                {
                    rs.close();
                }
                catch(SQLException se)
                {
                    se.printStackTrace();
                }
                rs=null;
            }
        }
        
        private void closeStatement(Statement stmt)
        {
         if(stmt!=null)
            {
                try
                {
                    stmt.close();
                }
                catch(SQLException se)
                {
                    se.printStackTrace();
                }
                stmt=null;
            }
        }
        
        private void closeConnection(Connection conn)
        {
         if(conn!=null)
            {
                try
                {
                    conn.close();
                }
                catch(SQLException se)
                {
                    se.printStackTrace();
                }
                conn=null;
            }
        }
    }这是源代码    
    要是在While里面再加一句  “rs.next();”  就能显示出第二条数据  但是也只能显示一条数据 
    不会把三条数据都显示出来。(总共三条数据) 啊啊啊。。纠结
      

  7.   

    你的代码我看了,好晕,太多重复的了!
    我知道你要实现什么功能,你把SQL语句打印出来看看!
      

  8.   

     String strSQL="select * from bookinfo where title like '%"+keyword+"%'";
          rs=stmt.executeQuery(strSQL);             
     貌似这里有个rs了嘛
      if(condition.equals("all"))
                {   rs=stmt.executeQuery("select *from bookinfo;");
                    printBookInfo(out,rs);
                    out.close();
                }rs这么多次的使用?         
      

  9.   

    没有多次使用RS啊   不是有IF语句吗? 大神告诉我怎么才能把暑假都读出来 只要他实现第一个“列出全部数据就”就好了
      

  10.   

    先把你SQL语句放在数据库中执行试试
      

  11.   

    select *from bookinfo; 在数据库可以读出3条数据,  While只读出了一条
      

  12.   

    有调试过没,看下是否是执行的all条件里面的,是不是执行到其他if语句里面了
    如果是执行了all条件里面的,调试看下传参之前的rs变量
      

  13.   

    StringBuffer sql = new String("select * from bookinfo where 1=1");
    if(null != condition && !"".equals(condition)){
        if("precision".equals(condition)){
             if(null != title && !"".equals(title))
                 sql.append(" and title='"+title+"'");
             if(null != author && !"".equals(author))
                 sql.append(" and author='"+author+"'");
             if(null != bookconcern && !"".equals(bookconcern))
                 sql.append(" and bookconcern='"+bookconcern+"'");
        } else if("keyword".equals(condition)){
             if(null != keyword && !"".equals(keyword))
                  sql.append(" and title like '%"+keyword+"%'");
        }

    上面的SQL就实现了你想要的功能,建议你把它改成我上面写的试下!
      

  14.   

    我去,我才发现我写的是 if(rs.next)  要改成while
    希望同学们注意一下:)))