一个简单的jsp分页实现

Thursday March 29, 2007

环境
xp + jdk1.5 + tomcat5.5.23 + eclipse3.2.2 + myeclipse5.1.1GA + sqlserver2000

使用连接池方式连接数据库,至于这方面的问题可参考 http://roamlog.cn/?p=39

写一个DBConnection.java封装对数据库的一些操作,实现SqlTestDS的getConnection()方法连接数据库

DBConnection.java

package database;
import java.sql.*;
public class DBConnection{
	private Connection conn = null;
	private Statement stmt = null;
	ResultSet rs = null;
	public ResultSet executeQuery(String sql) {
	try {
	     conn =SqlTestDS.getConnection();
             stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	public void executeUpdate(String sql) {
		try {
			conn = SqlTestDS.getConnection();
			stmt =conn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void closeStmt() {
		try {
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void closeConn() {
		try {
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


一个jsp文件,负责分页的处理
fenye.jsp

<%@ page contentType="text/html;charset=gbk"%>
<%@ page pageEncoding="gbk"%>
<%
request.setCharacterEncoding("gbk");
%>
<%@ page import="java.sql.*"%>
<%@ page import="database.DBConn"%>
 
<%
	//变量声明
	ResultSet sqlRst; //结果集对象
	String strCon; //数据库连接字符串
	String strSQL; //SQL语句
	int intPageSize; //一页显示的记录数
	int intRowCount; //记录总数
	int intPageCount; //总页数
	int intPage; //待显示页码
	String strPage;
	int i;
	//设置一页显示的记录数
	intPageSize = 4;
	//取得待显示页码
	strPage = request.getParameter("page");
	if (strPage == null) {
//表明在QueryString中没有page这一个参数,此时显示第一页数据
		intPage = 1;
	} else {//将字符串转换成整型
		intPage = Integer.parseInt(strPage);
		if (intPage < 1)
			intPage = 1;
	}
	strSQL = "select fkid,shjiaid from shop_shjiafk order by fkid desc";
	//执行SQL语句并获取结果集
	DBConn dd = new DBConn();
	sqlRst = dd.executeQuery(strSQL);
	//获取记录总数
	sqlRst.last();//光标在最后一行
	intRowCount = sqlRst.getRow();//获得当前行号
	//记算总页数
	intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
	//调整待显示的页码
	if (intPage > intPageCount)
		intPage = intPageCount;
%>
<html>
	<head>
		<script language="javascript">
function newwin(url) {
var newwin=window.open(url,"newwin","toolbar=no,location=no,directories=no,
status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=450");
newwin.focus();
return false;
}
</script>
		<script language="javascript">
function submit10()
{
self.location.replace("fenye.jsp")
}
</script>
         <meta http-equiv="Content-Type" content="text/html; charset=gbk">
		<title>会员管理</title>
	</head>
	<body>
		<form method="post" action="fenye.jsp">
			第
			<%=intPage%>
			页 共
			<%=intPageCount%>
			页
 
			<%
		if (intPage < intPageCount) {
		%>
			<a href="fenye.jsp?page=<%=intPage + 1%>">下一页 </a>
			<%
			}
			%>
			<%
			if (intPage > 1) {
			%>
			<a href="fenye.jsp?page=<%=intPage - 1%>"> 上一页</a>
			<%
			}
			%>
			转到第:
			<input type="text" name="page" size="8">
			页
			<span><input class=buttonface type="submit" value="go"
					name="cndok"> </span>
		</form>
		<table border="1" cellspacing="0" cellpadding="0">
			<tr>
				<th>
					ID
				</th>
				<th>
					用户名
				</th>
				<th width="8%">
					删除
				</th>
			</tr>
			<%
					if (intPageCount > 0) {
					//将记录指针定位到待显示页的第一条记录上
					sqlRst.absolute((intPage - 1) * intPageSize + 1);
					//显示数据
					i = 0;
					String user_id, user_name;
					while (i < intPageSize && !sqlRst.isAfterLast()) {
						user_id = sqlRst.getString(1);
						user_name = sqlRst.getString(2);
			%>
			<tr>
				<td>
					<%=user_id%>
				</td>
				<td>
					<%=user_name%>
				</td>
				<td width="8%" align="center">
					<a href="delete.jsp?user_id=<%=user_id%>"
						onClick="return newwin(this.href);">删除</a>
				</td>
			</tr>
			<%
						sqlRst.next();
						i++;
					}
				}
			%>
		</table>
	</body>
</html>
<%
	dd.closeStmt();
	dd.closeConn();
%>

完成

1 Comment

  1. 呵呵,帮你PP顶!也请来返利网看看……

Leave a reply