從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();
}
}
}
請閱讀此:http://theopentutorials.com/examples/java-ee/jsp/pagination-in-servlet-and-jsp/ –
看看[這個](http://stackoverflow.com/questions/ 17939431 /如何執行分頁在JSP) –