使用jsp和servlet显示来自mysql的数据

时间:2014-06-06 03:48:56

标签: java mysql database jsp servlets

从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();
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

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

例如,如果您想获得第3页,

/dataServlet?pageNumber=3

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

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

获取页码后,您可以构建要查询的SQL语句:

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

然后得到了结果。