【问题标题】:display data from mysql using jsp and servlet使用 jsp 和 servlet 显示来自 mysql 的数据
【发布时间】:2014-06-06 03:48:55
【问题描述】:

从 MySQL 数据库中,如何使用 JSP 和 servlet 根据动态创建的页面数量在每个页面上显示 10 行。

我在 servlet 中尝试过,但它只在一页中显示所有数据。

private void doProcess(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException , SQLException{
            Connection conn = null;
            Statement  stmt = null;
            ResultSet   rs = null;
            PrintWriter out = response.getWriter();
            try{
                Class.forName("com.mysql.jdbc.Driver").newInstance();

                System.out.println("Set connection : URL : jdbc:mysql://localhost:3306/post");
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/post","root","admin");

                System.out.println("Create statement.");
                stmt = conn.createStatement();

                //select all data from db
                rs = stmt.executeQuery("select * from postdata");          

                out.print("<HTML>");
                out.print("<HEAD>");
                out.print("<TITLE>Hello Connect Database</TITLE>");
                out.print("</HEAD>");
                out.print("<BODY>");
                out.println("<link rel='stylesheet' type='text/css' href='loadpostdata.css' />");                    

                out.print("<div width=100% >");
                while(rs.next()){
                   //  out.print("post_text : " + rs.getString("post_text"));
                   String postdata = rs.getString("post_text");
                   //out.print(postdata);
                   out.print(" <table border=1 bgcolor=yellow width =70% >");
                     out.print("<tr>");
                       out.print(" <td  width =70% height=200px width =70% > latest image yellow</td>   ");        
                     out.print(" </tr>   ");     
                     out.print(" <tr>");
                       out.print("<td  width =70% height=255px valign=top  width =70%>");
                   // out.print(" <textarea rows=15 cols=117 maxlength=1150 name=show_post_text_area value='+ postdata +' readonly></textarea>");
                   //request.setAttribute("show_post_text_area",postdata);
                         out.print("<div width=60% id=post_text>");
                           out.print(rs.getString("post_text"));
                         out.print("</div> ");
                       out.print("</td>");                                          
                     out.print(" </tr>    ");
                     out.print(" <div width=30% >");
                       out.print("  <iframe src=fb.html width=450 height=425 align=right>");
                         out.print("     <p>Your browser does not support iframes.</p>");
                       out.print(" </iframe>");
                     out.print("  </div>");                    
                   out.print("</table>");
                   out.print("<br>");
                   out.print("<br>");
                }
                out.print("</div> ");

                System.out.println("Get value from table postdata.");
                out.print("</BODY>");
                out.print("</HTML>");
                out.flush();
            }catch(ClassNotFoundException e){
                e.printStackTrace();
            finally{
                if(stmt != null){
                    stmt.close();
                }
                if(conn != null){
                    conn.close();
                }
            }
        }

【问题讨论】:

标签: java mysql database jsp servlets


【解决方案1】:

在您的 HTTP 请求中,应添加一个附加参数:pageNumber。

例如,如果要获取第 3 页,

/dataServlet?pageNumber=3

在您的 serverlet 代码中,您可以从

获取 pageNumber
request.getParameter("pageNumber").

获取页码后,可以构建SQL语句进行查询:

String sql = "select ... from .... limit 10 offset " + (pageNumber - 1) * 10

那么就得到了结果。

【讨论】:

  • 但是我怎样才能创建另一个网页呢?不是按照代码,但从逻辑上讲,我可以从数据库中获取行数,例如表中的 28 行,所以我如何动态创建新网页,它可以显示接下来的 10 条记录,依此类推,直到最后没有。带有导航的表格中的行数?
  • 如果你有网页以外的其他逻辑,请分享。
猜你喜欢
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 2015-02-07
  • 2015-05-30
  • 1970-01-01
相关资源
最近更新 更多