JSP分页 sqlserver数据库 超详细
2015-01-08 23:45
169 查看
DAO 层
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import Util.DBManager;
import Entity.Userinfo;
public class getuser {
private ResultSet rst=null;
private PreparedStatement pst=null;
private Connection conn=null;
DBManager dbmanager=new DBManager();
List<Userinfo> userlist=new ArrayList<Userinfo>();
int pageSize=5;//每页的记录数
int rowCount=0;//记录总行数
int pageCount=0;//页数
public List<Userinfo> getalluser(int pageNow)//传入一个(参数)当前页数,返回对应的pageSize(这里是5,可以设置)条结果
{
conn=dbmanager.getConn();
Userinfo user=null;
try {
String sql="select top "+pageSize+" * from myinfo where id not in(select top "+pageSize*(pageNow-1)+" id from myinfo)";//返回当前页对应的记录数
pst=conn.prepareStatement(sql);//将SQL语句加入预编译
rst= pst.executeQuery();//执行返回结果集
while(rst.next())//循环
{
user=new Userinfo();//将结果封装到实体类
user.setId(rst.getInt(1));//获取从数据库查到的对应的字段
user.setUsername(rst.getString(2));//获取从数据库查到的对应的字段
user.setUserpass(rst.getString(3));//获取从数据库查到的对应的字段
userlist.add(user);//循环将封装后的实体添加到lists
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
dbmanager.closeAll(conn, pst, rst);//关闭连接
}
return userlist;//返回当前页对应的所有数据(经过封装后的ResultSet记录)
}
public int pageCount()//通过总记录数计算出总页数并返回出去
{
conn= dbmanager.getConn();
try {
String sql="select count(*) from myinfo";//返回数据库的总记录数(总行数)
pst=conn.prepareStatement(sql);
rst=pst.executeQuery();
if(rst.next())//单条记录用if,多条记录循环用while
{
rowCount= rst.getInt(1);//计算总的记录条数
}
if(rowCount/pageSize==0)//总行数/每页的行数=页数
{
pageCount=rowCount/pageSize;//页数
}
else
{
pageCount=rowCount/pageSize+1;//页数+1(有多余的则页数加1)
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
dbmanager.closeAll(conn, pst, rst);//关闭连接
}
return pageCount;
}
}
Entity 层
package Entity;
public class Userinfo {
private int id;
public Userinfo() {
}
public Userinfo(int id, String username, String userpass) {
this.id = id;
this.username = username;
this.userpass = userpass;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
private String username;
private String userpass;
}
Util层
package Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DBManager {
private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=userinfo";
private static final String user = "sa";
private static final String pwd = "123456";
Connection conn = null;
public Connection getConn() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
System.out.println("驱动连接成功");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public void closeAll(Connection conn, Statement sm, ResultSet rs) {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (sm != null) {
sm.close();
sm = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
index.jsp 页面
<%@page import="Entity.Userinfo"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="DAO.getuser"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>jsp记录分页效果</h3>
<%
int pageNow=1;//初始化第一页记录
getuser dao=new getuser();
String getpageNow=request.getParameter("pageNow");
if(getpageNow!=null)//获取点击传入的页数(当前页)
{
pageNow=Integer.parseInt(getpageNow);
}
List userlist= dao.getalluser(pageNow ); //获取当前页面对应的记录
%>
<table>
<tr>
<td>用户编号:</td>
<td>用户名:</td>
<td>密码:</td>
</tr>
<%
for(int i=0;i<userlist.size();i++)//循环打印
{
Userinfo user=(Userinfo)userlist.get(i);//循环打印
%>
<tr>
<td><%=user.getId()%></td>
<td><%=user.getUsername()%></td>
<td><%=user.getUserpass()%></td>
</tr>
<%
}
%>
</table>
<%
int pageCount=dao.pageCount();//通过SQL语句获取所有的记录页面
if(pageNow!=1)
{
out.print("<a href=index.jsp?pageNow="+(pageNow-1)+">上一页</a>");
}
out.print("<a href=index.jsp?pageNow="+1+">首页</a>");
for(int i=1;i<pageCount;i++)
{
out.print("<a href=index.jsp?pageNow="+i+">["+i+"]</a>");
}
out.print("<a href=index.jsp?pageNow="+pageCount+">尾页</a>");
if(pageNow!=pageCount)
{
out.print("<a href=index.jsp?pageNow="+(pageNow+1)+">下一页</a>");
}
%>
</body>
</html>
相关文章推荐
- ZT:详细的jsp分页(oracle+jsp+apache)
- 详细的jsp分页(oracle+jsp+apache)
- jsp分页显示的代码与详细步骤
- 分页查询JSP页面查询列表详细内容
- jsp分页显示的代码与详细步骤 复制代码
- 第一次做的jsp分页,详细代码。。。。
- jsp分页显示的代码与详细步骤
- 详细的jsp分页(oracle+jsp+apache)
- JSP分页技术实现
- 目前最好的JSP分页技术
- JSP动态网站开发环境配置详细方法第1/2页
- jsp实现分页(限定显示指定页数)和页面跳转功能
- 利用JDBC显示数据库详细信息---JSP实现
- jsp 分页
- JSP数据库数据分页显示
- JSP标签之pager-taglib分页标签
- hibernate+struts2的详细分页代码
- jsp分页显示
- 在JSP页面中实现检索数据的分页显示
- JSP分页