我正在做一个分页的内容,有些问题实在解决不了,所以请教各位高手帮帮忙!
我将代码分段贴出,请各位抽空帮忙看一下。
第一段代码是分页类的源代码
/**
*
*/
package com.connectionbean;import java.sql.*;
import java.util.*;/**
* @author admin
* 实现页面分页的业务逻辑类
*/
public class PageBean { /** 当前第几页 */
public int curPage = 1; /** 总页数,一共有多少页 */
public int maxPage; /** 总的行数,一共有多少行 */
public int maxRowCount; /** 每页固定20行 */
public int rowsPerPage = 20; private Connection conn = null; public Vector data; /**
* @return curPage
*/
public int getCurPage() {
return curPage;
} /**
* @param curPage
* 要设置的 curPage
*/
public void setCurPage(int curPage) {
this.curPage = curPage;
} /**
* @return maxPage
*/
public int getMaxPage() {
return maxPage;
} /**
* @param maxPage
* 要设置的 maxPage
*/
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
} /**
* @return maxRowCount
*/
public int getMaxRowCount() {
return maxRowCount;
} /**
* @param maxRowCount
* 要设置的 maxRowCount
*/
public void setMaxRowCount(int maxRowCount) {
this.maxRowCount = maxRowCount;
} public PageBean() {
try {
this.setPageBean();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
} /**
* @param page
* @return
*/
public PageBean getResult(String page) {
PageBean pageBean = null;
/*Newinfo newinfo = null;*/
try {
pageBean = new PageBean();
Vector<Object[]> v = new Vector<Object[]>();
int pageNum = Integer.parseInt(page);
/*DBConnection connection = new DBConnection();
conn = connection.getConnection();*/
Statement stmt = conn.createStatement();
String strSql = "select top " + pageNum * pageBean.rowsPerPage
+ "* from news_info";
ResultSet rs = stmt.executeQuery(strSql);
int i = 0;
while (rs.next()) {
if (i > ((pageNum - 1) * pageBean.rowsPerPage - 1)) {
/*newinfo = new Newinfo();*/
Object obj[] = new Object[2];
obj[0] = rs.getString(3);
obj[1] = rs.getString(7);
v.add(obj);
/*newinfo.setTitle(rs.getString(3));
newinfo.setUp_time(rs.getDate(7));
v.add(newinfo);*/
}
i++;
}
rs.close();
stmt.close();
pageBean.setCurPage(pageNum);
pageBean.data = v;
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return pageBean;
} /**
* 得到总行数的方法
* @return
*/
public int getAvailableCount() {
int ret = 0;
try {
DBConnection connection = new DBConnection();
conn = connection.getConnection();
Statement stmt = conn.createStatement();
String strSql = "select * from news_info";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
ret++;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return ret;
} /**
* 得到总页数的方法
*/
public void setPageBean() {
// 得到总行数
this.setMaxRowCount(this.getAvailableCount());
//得到总页数的方法
if (this.maxRowCount % this.rowsPerPage == 0) {
this.maxPage = this.maxRowCount / this.rowsPerPage;
} else {
this.maxPage = this.maxRowCount / this.rowsPerPage + 1;
}
}
}
----------------------------------------
第二段代码是Servlet的源代码
package com.controller;import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.connectionbean.PageBean;
@SuppressWarnings("serial")
public class ForumServlet extends HttpServlet {
private static final String CONTENT_TYPE="text/html;charset=GBK"; /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try {
PageBean page1 = new PageBean();
PageBean page2 = page1.getResult((String)request.getParameter("jumpPage"));
//把PageBean保存到request对象中
request.setAttribute("page2",page2);
} catch (RuntimeException e) {
out.println(e.getMessage());
}
RequestDispatcher dis = request.getRequestDispatcher("news_show.jsp");
dis.forward(request, response);
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
}
我将代码分段贴出,请各位抽空帮忙看一下。
第一段代码是分页类的源代码
/**
*
*/
package com.connectionbean;import java.sql.*;
import java.util.*;/**
* @author admin
* 实现页面分页的业务逻辑类
*/
public class PageBean { /** 当前第几页 */
public int curPage = 1; /** 总页数,一共有多少页 */
public int maxPage; /** 总的行数,一共有多少行 */
public int maxRowCount; /** 每页固定20行 */
public int rowsPerPage = 20; private Connection conn = null; public Vector data; /**
* @return curPage
*/
public int getCurPage() {
return curPage;
} /**
* @param curPage
* 要设置的 curPage
*/
public void setCurPage(int curPage) {
this.curPage = curPage;
} /**
* @return maxPage
*/
public int getMaxPage() {
return maxPage;
} /**
* @param maxPage
* 要设置的 maxPage
*/
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
} /**
* @return maxRowCount
*/
public int getMaxRowCount() {
return maxRowCount;
} /**
* @param maxRowCount
* 要设置的 maxRowCount
*/
public void setMaxRowCount(int maxRowCount) {
this.maxRowCount = maxRowCount;
} public PageBean() {
try {
this.setPageBean();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
} /**
* @param page
* @return
*/
public PageBean getResult(String page) {
PageBean pageBean = null;
/*Newinfo newinfo = null;*/
try {
pageBean = new PageBean();
Vector<Object[]> v = new Vector<Object[]>();
int pageNum = Integer.parseInt(page);
/*DBConnection connection = new DBConnection();
conn = connection.getConnection();*/
Statement stmt = conn.createStatement();
String strSql = "select top " + pageNum * pageBean.rowsPerPage
+ "* from news_info";
ResultSet rs = stmt.executeQuery(strSql);
int i = 0;
while (rs.next()) {
if (i > ((pageNum - 1) * pageBean.rowsPerPage - 1)) {
/*newinfo = new Newinfo();*/
Object obj[] = new Object[2];
obj[0] = rs.getString(3);
obj[1] = rs.getString(7);
v.add(obj);
/*newinfo.setTitle(rs.getString(3));
newinfo.setUp_time(rs.getDate(7));
v.add(newinfo);*/
}
i++;
}
rs.close();
stmt.close();
pageBean.setCurPage(pageNum);
pageBean.data = v;
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return pageBean;
} /**
* 得到总行数的方法
* @return
*/
public int getAvailableCount() {
int ret = 0;
try {
DBConnection connection = new DBConnection();
conn = connection.getConnection();
Statement stmt = conn.createStatement();
String strSql = "select * from news_info";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
ret++;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return ret;
} /**
* 得到总页数的方法
*/
public void setPageBean() {
// 得到总行数
this.setMaxRowCount(this.getAvailableCount());
//得到总页数的方法
if (this.maxRowCount % this.rowsPerPage == 0) {
this.maxPage = this.maxRowCount / this.rowsPerPage;
} else {
this.maxPage = this.maxRowCount / this.rowsPerPage + 1;
}
}
}
----------------------------------------
第二段代码是Servlet的源代码
package com.controller;import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.connectionbean.PageBean;
@SuppressWarnings("serial")
public class ForumServlet extends HttpServlet {
private static final String CONTENT_TYPE="text/html;charset=GBK"; /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try {
PageBean page1 = new PageBean();
PageBean page2 = page1.getResult((String)request.getParameter("jumpPage"));
//把PageBean保存到request对象中
request.setAttribute("page2",page2);
} catch (RuntimeException e) {
out.println(e.getMessage());
}
RequestDispatcher dis = request.getRequestDispatcher("news_show.jsp");
dis.forward(request, response);
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货