jsp分頁功能的實現
package Fen;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.CORBA.PUBLIC_MEMBER;
import myshop.MysqlMY;
public class FenYe extends HttpServlet {
int pageNow = 1;
int pageSize = 3;
int rowCount = 0;
int pageCount = 0;
Connection cn;
PreparedStatement ps;
ResultSet rs;
MysqlMY mysqlMY ;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//使用資料庫得到頁的記錄 rowCount
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String aString = (String)(request.getParameter("pageNow"));
if(aString!=null){
pageNow = Integer.parseInt(aString);
}
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY >");
out.println("<script type='text/javascript' laguage='javascript'>");
out.println("function ok(){var "
+ "pageNow = document.getElementById('tiao')"
+ "window.alert(pageNow.value);"
+ "window.open('FenYe?pageNow='+pageNow.value+,'_self'}");
out.println("</script>");
out.println(" <br/><br/><br/><br/><br/><br/>");
out.println(" <center>");
out.println("<form>");
//設計一個方法得到我們的到rowCout
mysqlMY = new MysqlMY();
cn = mysqlMY.getConnection();
try {
ps = cn.prepareStatement("select count(*) from book");
rs =ps.executeQuery();
if(rs.next()){
rowCount = rs.getInt(1);
out.println("共有");
out.println(rowCount);
out.println("本書");
out.println(" <br/>");
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null){rs.close();}
if(ps!=null){ps.close();}
if(cn!=null){cn.close();}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//得到頁數pageCount
if(rowCount%pageSize==0){
pageCount = rowCount/pageSize;
}else{
pageCount = rowCount/pageSize+1;
}
out.println(" <table border="+1+" cellspacing="+0+" width=600 height= 200 >");
out.println(" <tr align= center>");
out.println(" <td>id</td>");
out.println(" <td>書名</td>");
out.println(" <td>出版社</td>");
out.println(" <td>作者</td>");
out.println(" <td>價錢</td>");
out.println(" <td>數量</td>");
out.println(" </tr>");
//顯示我們的頁數
try {
cn =mysqlMY.getConnection();
// ps = cn.prepareStatement("select * from book limit 0,3");
ps = cn.prepareStatement("select * from book limit "+((pageNow)*3-3)+","+pageSize);
//ps.setInt(1, pageNow);
//ps.setInt(2, pageNow * pageSize - pageSize);
rs = ps.executeQuery();
//分頁顯示千萬不能寫成if
while(rs.next()){
out.println(" <tr align= center>");
out.println("<td>"+rs.getInt(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"</td>");
out.println("<td>"+rs.getString(4)+"</td>");
out.println("<td>"+rs.getInt(5)+"</td>");
out.println("<td>"+rs.getInt(6)+"</td>");
out.println(" </tr>");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println(" </table>");
out.println("<br/>");
if(pageNow!=1){
out.println("<a href='FenYe?pageNow="+(pageNow-1)+"'>前一頁</a>");
}
for (int i = pageNow+1; i <=pageNow+2; i++) {
out.println("<a href='FenYe?pageNow="+i+"'>"+i+"</a>");
}
if(pageNow!=pageCount){
out.println("<a href='FenYe?pageNow="+(pageNow+1)+"'>下一頁</a>");
}
out.println("當前頁:"+pageNow+"總頁數:"+rowCount);
out.println("跳轉到 :<input typw='text' name='tiaot' id='tiao' /><input type='button' name='tiaob' value='跳轉'onClick = 'ok()'>");
out.println("</form>");
out.println(" </center");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void destroy() {
super.destroy();
}
public void init() throws ServletException {
// Put your code here
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.omg.CORBA.PUBLIC_MEMBER;
import myshop.MysqlMY;
public class FenYe extends HttpServlet {
int pageNow = 1;
int pageSize = 3;
int rowCount = 0;
int pageCount = 0;
Connection cn;
PreparedStatement ps;
ResultSet rs;
MysqlMY mysqlMY ;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//使用資料庫得到頁的記錄 rowCount
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String aString = (String)(request.getParameter("pageNow"));
if(aString!=null){
pageNow = Integer.parseInt(aString);
}
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY >");
out.println("<script type='text/javascript' laguage='javascript'>");
out.println("function ok(){var "
+ "pageNow = document.getElementById('tiao')"
+ "window.alert(pageNow.value);"
+ "window.open('FenYe?pageNow='+pageNow.value+,'_self'}");
out.println("</script>");
out.println(" <br/><br/><br/><br/><br/><br/>");
out.println(" <center>");
out.println("<form>");
//設計一個方法得到我們的到rowCout
mysqlMY = new MysqlMY();
cn = mysqlMY.getConnection();
try {
ps = cn.prepareStatement("select count(*) from book");
rs =ps.executeQuery();
if(rs.next()){
rowCount = rs.getInt(1);
out.println("共有");
out.println(rowCount);
out.println("本書");
out.println(" <br/>");
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null){rs.close();}
if(ps!=null){ps.close();}
if(cn!=null){cn.close();}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//得到頁數pageCount
if(rowCount%pageSize==0){
pageCount = rowCount/pageSize;
}else{
pageCount = rowCount/pageSize+1;
}
out.println(" <table border="+1+" cellspacing="+0+" width=600 height= 200 >");
out.println(" <tr align= center>");
out.println(" <td>id</td>");
out.println(" <td>書名</td>");
out.println(" <td>出版社</td>");
out.println(" <td>作者</td>");
out.println(" <td>價錢</td>");
out.println(" <td>數量</td>");
out.println(" </tr>");
//顯示我們的頁數
try {
cn =mysqlMY.getConnection();
// ps = cn.prepareStatement("select * from book limit 0,3");
ps = cn.prepareStatement("select * from book limit "+((pageNow)*3-3)+","+pageSize);
//ps.setInt(1, pageNow);
//ps.setInt(2, pageNow * pageSize - pageSize);
rs = ps.executeQuery();
//分頁顯示千萬不能寫成if
while(rs.next()){
out.println(" <tr align= center>");
out.println("<td>"+rs.getInt(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"</td>");
out.println("<td>"+rs.getString(4)+"</td>");
out.println("<td>"+rs.getInt(5)+"</td>");
out.println("<td>"+rs.getInt(6)+"</td>");
out.println(" </tr>");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println(" </table>");
out.println("<br/>");
if(pageNow!=1){
out.println("<a href='FenYe?pageNow="+(pageNow-1)+"'>前一頁</a>");
}
for (int i = pageNow+1; i <=pageNow+2; i++) {
out.println("<a href='FenYe?pageNow="+i+"'>"+i+"</a>");
}
if(pageNow!=pageCount){
out.println("<a href='FenYe?pageNow="+(pageNow+1)+"'>下一頁</a>");
}
out.println("當前頁:"+pageNow+"總頁數:"+rowCount);
out.println("跳轉到 :<input typw='text' name='tiaot' id='tiao' /><input type='button' name='tiaob' value='跳轉'onClick = 'ok()'>");
out.println("</form>");
out.println(" </center");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void destroy() {
super.destroy();
}
public void init() throws ServletException {
// Put your code here
}
}
相關文章
- 分頁功能的實現
- DataGridView分頁功能的實現View
- [SQL Server]分頁功能的實現SQLServer
- 使用 jQuery 實現分頁功能jQuery
- web 實現分頁列印功能Web
- Java Web(十一) 分頁功能的實現JavaWeb
- DjangoRestFramework 實現分頁功能與搜尋功能DjangoRESTFramework
- 文章內容分頁功能實現
- Vue + element.ui table 分頁功能+搜尋功能的實現VueUI
- 將分頁功能從JSP頁面中獨立出來JS
- JSP怎樣將查詢的資料實現分頁操作JS
- aspnetpager+repeater+oracle實現分頁功能Oracle
- WPF DataGrid分頁功能實現程式碼
- 分頁的實現
- JSP中實現遮蔽次的功能JS
- Vue + Element UI + Lumen 實現通用表格功能 - 分頁VueUI
- struts2.0+E3.Table0.8分頁功能的實現
- JSP模板繼承功能實現JS繼承
- JSP註冊驗證功能的實現JS
- 看看JSP中怎樣實現分頁顯示(僱員管理)JS
- Hibernate 的分頁實現
- MyBatis實現分頁的方式MyBatis
- elementUI實現分頁UI
- ASP.NET中Repeater控制元件實現分頁功能ASP.NET控制元件
- JSP(ajax)+Servlet實現簡單的登入功能JSServlet
- vue + axios 實現分頁引數傳遞,高階搜尋功能實現VueiOS
- 滑動視窗式分頁的實現
- Spring Boot入門系列(十六)使用pagehelper實現分頁功能Spring Boot
- Java Web 分頁實現JavaWeb
- Mybatis分頁實現流程MyBatis
- jsp定製分頁標籤JS
- webscoket+jsp實現簡單的多人聊天頁面WebJS
- DjangoRestFramework框架三種分頁功能的實現 - 在DjangoStarter專案模板中封裝DjangoRESTFramework框架封裝
- 【清清月兒】用SQL 2005的ROW_NUMBER() 實現分頁功能SQL
- 關於黑馬旅遊網的實現 --- 分頁查詢功能,點選分頁碼不顯示資料
- iOS 類知乎”分頁”效果的實現?iOS
- MYSQL SQLServer分頁查詢的實現MySqlServer
- Repeater控制元件的分頁實現控制元件