jsp 对数据库取出的数据分页显示 简单点的 学习用的
2个回答
展开全部
/*
* jsp+java+sqlserver
* 部署可运行
* 数据库sql2005
* 数据只有3列 ID UserName UserPass
*
*/
//java code 分页查询处理类 UserDAO.java
package com.fy.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.chol.bean.User;
import com.fy.util.DBHelper;
public class UserDao {
java.sql.Connection con = null;
java.sql.ResultSet rs = null;
java.sql.PreparedStatement pst = null;
public static final int Num = 5; //定义常量来设定页面显示数据的条数
public List<User> QueryUserInfoByPage(int page){
int number = 0;
if(page>0){
number = Num*(page - 1);
}
String sql = "select top "+ Num +" * from tb_user where " +
"userId not in(select top "+number+" userId from tb_user " +
"order by userId) order by userId";
try {
con = DBHelper.GetConn();
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
ArrayList list = new ArrayList();
while(rs.next()){
User us = new User();
us.setUserId(rs.getInt("userId"));
us.setUserName(rs.getString("userName"));
us.setUserPass(rs.getString("userPass"));
list.add(us);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
finally{
DBHelper.close(con, rs, pst);
}
}
}
// jsp code 前台页面展示 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.fy.dao.UserDao"%>
<%@page import="com.chol.bean.User"%>
<%
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>
<%
UserDao usDao = new UserDao();
int number = 1;
int pageSize = UserDao.Num;
String pageNumber = request.getParameter("Page");
if(pageNumber != null){
number = Integer.parseInt(pageNumber);
}
List<User> userlist = usDao.QueryUserInfoByPage(number);
int prePage = number;
int nextPage = number;
if(userlist.size() == pageSize)
nextPage = number +1;
if(number > 1)
prePage = number -1;
%>
<body><%=pageSize %> oo <%=prePage %> oo <%=nextPage %> 77 <%=pageNumber %> 55
<form action="">
<table>
<tr>
<td>编号</td>
<td>姓名</td>
</tr>
<% for(int i =0 ; i<userlist.size();i++){
User us = userlist.get(i);
%>
<tr>
<td><%= us.getUserId() %></td>
<td><%= us.getUserName() %></td>
</tr><% } %>
</table>
<p><a href="index.jsp?Page=<%=prePage %>">上一页</a> <a href="index.jsp?Page=<%=nextPage %>">下一页</a></p>
</form> </body>
</html>
* jsp+java+sqlserver
* 部署可运行
* 数据库sql2005
* 数据只有3列 ID UserName UserPass
*
*/
//java code 分页查询处理类 UserDAO.java
package com.fy.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.chol.bean.User;
import com.fy.util.DBHelper;
public class UserDao {
java.sql.Connection con = null;
java.sql.ResultSet rs = null;
java.sql.PreparedStatement pst = null;
public static final int Num = 5; //定义常量来设定页面显示数据的条数
public List<User> QueryUserInfoByPage(int page){
int number = 0;
if(page>0){
number = Num*(page - 1);
}
String sql = "select top "+ Num +" * from tb_user where " +
"userId not in(select top "+number+" userId from tb_user " +
"order by userId) order by userId";
try {
con = DBHelper.GetConn();
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
ArrayList list = new ArrayList();
while(rs.next()){
User us = new User();
us.setUserId(rs.getInt("userId"));
us.setUserName(rs.getString("userName"));
us.setUserPass(rs.getString("userPass"));
list.add(us);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
finally{
DBHelper.close(con, rs, pst);
}
}
}
// jsp code 前台页面展示 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.fy.dao.UserDao"%>
<%@page import="com.chol.bean.User"%>
<%
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>
<%
UserDao usDao = new UserDao();
int number = 1;
int pageSize = UserDao.Num;
String pageNumber = request.getParameter("Page");
if(pageNumber != null){
number = Integer.parseInt(pageNumber);
}
List<User> userlist = usDao.QueryUserInfoByPage(number);
int prePage = number;
int nextPage = number;
if(userlist.size() == pageSize)
nextPage = number +1;
if(number > 1)
prePage = number -1;
%>
<body><%=pageSize %> oo <%=prePage %> oo <%=nextPage %> 77 <%=pageNumber %> 55
<form action="">
<table>
<tr>
<td>编号</td>
<td>姓名</td>
</tr>
<% for(int i =0 ; i<userlist.size();i++){
User us = userlist.get(i);
%>
<tr>
<td><%= us.getUserId() %></td>
<td><%= us.getUserName() %></td>
</tr><% } %>
</table>
<p><a href="index.jsp?Page=<%=prePage %>">上一页</a> <a href="index.jsp?Page=<%=nextPage %>">下一页</a></p>
</form> </body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询