簡體   English   中英

使用jsp和servlet顯示來自mysql的數據

[英]display data from mysql using jsp and servlet

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

在您的HTTP請求中,應該添加一個附加參數:pageNumber。

例如,如果要獲取第三頁,

/dataServlet?pageNumber=3

在您的serverlet代碼中,您可以從

request.getParameter("pageNumber").

獲取頁碼后,您可以構建要查詢的SQL語句:

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

然后得到結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM