2014-06-06 39 views
0

從MySQL數據庫中,如何在每個頁面上顯示10行,使用JSP和servlet並根據動態創建的頁面數進行導航。使用jsp和servlet從mysql中顯示數據

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

請閱讀此:http://theopentutorials.com/examples/java-ee/jsp/pagination-in-servlet-and-jsp/ –

+0

看看[這個](http://stackoverflow.com/questions/ 17939431 /如何執行分頁在JSP) –

回答

0

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

例如,如果你想獲得的第3頁,

/dataServlet?pageNumber=3 

而在你serverlet代碼,你可以從

request.getParameter("pageNumber"). 

的PAGENUMBER後你得到的頁碼,你可以建立SQL查詢語句:

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

然後得到結果。

+0

但我怎樣才能創建另一個網頁? 不是每個代碼,但從邏輯上我可以從數據庫中獲取行數例如28行在表中,所以我如何創建新的網頁動態,可以顯示下10條記錄等等,直到結束不。從導航表的行? –

+0

如果你有任何其他的邏輯,除了網頁,然後請分享。 –

相關問題