今天编了一个MVC模式数据库分页的程序拿出来和大家分享一下!!
以下程序是在tomcat5.5,SQL server2000,servlet2.4,jsp2.0 中调试的
1.在SQL中建立数据库,字段(标题,主题,日期)
相信大家对数据库的建立应该是再熟悉不过了
2.先建个数据库连接的类:
package database;
import java.sql.*;
public class DataConn {
Connection con;
public DataConn()
{
}
public Connection getCon() //返回Connection连接的方法
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};server=.;database=myhome","","");
return con;
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return null;
}
}
3.再建立个用于分页的javaBean:
package database;
import java.sql.*;
import java.util.*;
public class UseBean {
private int curtpage=1; //当前页
private int allpage; //一供多少页
public int rowspage=5; //一页多少记录
private int allrows; //共多少记录
public Vector data;
public UseBean () throws Exception
{
this.setBean(); //在这初始化一下是为了在JSP页面中一开始就能得到数据库中的记录
}
public int getCurtpage()
{
return curtpage;
}
public void setCurtpage(int curtpage)
{
this.curtpage=curtpage;
}
public int getAllpage()
{
return allpage;
}
public void setAllpage(int allpage)
{
this.allpage=allpage;
}
public int getAllrows()
{
return allrows;
}
public void setAllrows(int allrows)
{
this.allrows=allrows;
}
public int getallpages() throws Exception
{
//求出一共多少页
String sql = "select * from article";
DataConn con = new DataConn();
Connection conn= con.getCon();
Statement stmt = conn.createStatement();
ResultSet st = stmt.executeQuery(sql);
int i=0;
while(st.next())
{
i++;
}
return i;
}
public UseBean getResultpage(String page) throws Exception //得到要显示的那一页的记录
{
UseBean ub= new UseBean();
int pagenum = Integer.parseInt(page);
String sql="select top "+pagenum*ub.rowspage+" * from article"; //得到是要显示在本页的及以前的数据记录之各
Vector v = new Vector(); // 定义一个数据集
DataConn con =new DataConn();
Connection conn = con.getCon();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
int i=0;
while(rs.next())
{
if(i>(pagenum-1)*ub.rowspage-1) //控制只把本页的数据赋到object对象中
{
Object[] object = new Object [3];
object [0] = rs.getString(1);
object [1] = rs.getString(2);
object [2] = rs.getString(3);
v.add(object);
}
i++;
}
rs.close();
stmt.close();
ub.setCurtpage(pagenum); //设置当前为第几页
ub.data=v; //把数据集V赋给数据集data
return ub;
}
public void setBean() throws Exception //设置总行数和设置总业数
{
this.setAllrows(this.getallpages()); //设置总行数
if(this.allrows%this.rowspage==0)
{
this.allpage = this.allrows/this.rowspage; //设置总页数:正好是整数倍的
}
else
{
this.allpage=this.allrows/this.rowspage+1; //设置总页数: 不是整数倍的
}
}
}
4.建立用于分页的JSP界面:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<jsp:useBean id="page2" class="database.UseBean" scope="request" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
<script language="javascript" >
function Jumping()
{
document.form1.submit();
return ;
}
function gotopage(pagenum)
{
document.form1.JumpPage.value=pagenum;
document.form1.submit();
return;
}
</script>
</head>
<body>
<table align="center" border="1">
<tr>
<td>主题</td>
<td>作者</td>
<td>日期</td>
</tr>
<%
String s =String.valueOf(page2.getCurtpage());
java.util.Vector v=page2.getResultpage(s).data;
java.util.Enumeration e =v.elements(); //elements把集合转换成枚举并赋给枚举e; Enumeration定义枚举
while(e.hasMoreElements())
{
Object [] object =(Object[]) e.nextElement();
%>
<tr>
<td><%=object[0] %></td>
<td><%=object[1] %></td>
<td><%=object[2] %></td>
</tr>
<%} %>
</table>
<form action="Pageservlet" method ="post" name="form1">
<p align="center">每页<%=page2.rowspage %>行 共<%=page2.getAllrows() %>行 第<%=page2.getCurtpage() %>页 共<%=page2.getAllpage() %>页</p>
<p align="center">
<%
if(page2.getCurtpage()==1)
{
out.print("首页 上一页");
}
else
{
%>
<a href="javascript:gotopage(<%=page2.getCurtpage() %>)">首页</a>
<a href="javascript:gotopage(<%=page2.getCurtpage()-1 %>)">上一页</a>
<%}%>
<%
if(page2.getCurtpage()==page2.getAllpage())
{
out.println("下一页 尾页 ");
}
else
{
%>
<a href="javascript:gotopage(<%=page2.getCurtpage()+1 %>)">下一页</a>
<a href="javascript:gotopage(<%=page2.getAllpage() %>)">尾页</a>
<%} %>
<select name="JumpPage" onchange="Jumping()">
<%
for(int i=1;i<=page2.getAllpage();i++)
{
if(page2.getCurtpage()==i)
{
%>
<option selected=<%=i %>><%=i %> </option>
<%}
else
{
%>
<option value=<%=i %>><%=i %> </option>
<%
}
}
%>
</select>
</p>
</form>
</body>
</html>
5.最后是建立servlet了:
package database;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Servlet implementation class for Servlet: Pageservlet
*
*/
public class Pageservlet extends HttpServlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public Pageservlet() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType( "text/html; charset=GBK");
response.setCharacterEncoding("GBK");
PrintWriter p = response.getWriter();
try
{
String page = request.getParameter("JumpPage");
UseBean page1 = new UseBean();
UseBean page2 = page1.getResultpage(page);
request.setAttribute("page2", page2);
}
catch(Exception ex)
{
ex.printStackTrace();
}
javax.servlet.RequestDispatcher dis=request.getRequestDispatcher("pageview.jsp"); //跳转到pageview.jsp页
dis.forward(request,response);
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}