package com.hu;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
public class Test {
int PageSize;
private Connection conn;
private ResultSet rs;
ResultSet count_rs;
private List list;
public int intCountic;
public int intPageSize;
public int intPageCount;
public int intPage;
public String nowPage;
public String HttpFile;
//取得网址是的参数
String PageParameter;
public Test() {
conn = null;
rs = null;
count_rs = null;
intCountic = 0;
intPage = 1;
intPageSize = 20;
}
//数据库连接
public Connection openConnection(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora", "ora", "ora");
}catch(Exception e){
System.out.println("连接数据库失败:"+e.toString());
e.printStackTrace();
}
return conn;
}
//普通查询 ,返回Result
public ResultSet jquery(String sql) {
Statement stmt = null;
try {
conn = openConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
// 返回List类型的结果集
public List jlist(String sql, String DataKeyField,
HttpServletRequest request){
//每页记录数
if(PageSize==0)
PageSize=20;
intPageSize = this.PageSize;
list = new ArrayList();
conn = null;
Statement stmt = null;
rs = null;
try {
conn = openConnection();
stmt = conn.createStatement();
//获得url,如 /user/china.jsp
HttpFile = request.getRequestURI();
//获得当前页数
nowPage = request.getParameter("pages");
//取得网址是的参数
PageParameter=this.urlPath(request);
//如果没有pages参数,则让页数为1
if (nowPage == null) {
intPage = 1;
}else {
intPage = Integer.parseInt(nowPage);
if (intPage < 1) {
intPage = 1;
}
}
//改造sql语句,从而得到总行数
String Csql = getCountJsql(sql, DataKeyField);
count_rs = stmt.executeQuery(Csql);
if (count_rs.next()) {
intCountic = count_rs.getInt(1);
}
if(count_rs!=null)
count_rs.close();
//得到分页总数
intPageCount = ((intCountic + intPageSize) - 1) / intPageSize;
if (intPage > intPageCount) {
intPage = intPageCount;
}
//改造sql语句,得到分页结果
sql = getJsql(sql, DataKeyField);
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
Map map;
int j=(intPage-1)*this.intPageSize+1;
for (; rs.next(); list.add(map)) {
map = new HashMap();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
map.put(rsmd.getColumnName(i).toLowerCase(), rs.getString(i));
//System.out.println("列名:"+rsmd.getColumnName(i)+" 值:"+rs.getString(i));
}
map.put("序号",String.valueOf(j));
j++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if(conn!=null)
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return list;
}
//显示分页内容
public String PageFooter() {
if(getCountic()<=0){
return "<span style=\"color:#FF3300;font-size:14px;\">对不起,暂无记录!</span>";
}
String style ="<style> \n";
style += ".page {color: #333333; background-color: #F3F3F3; height: 18px;width: 36px;";
style += "border: 1px solid #333333;margin-right: 1px; margin-left: 1px;} \n";
style += " .fytd {font-size: 12px; color: #333333;}\n";
style += ".fy:link {color: #333333;text-decoration: underline;font-size: 12px;} \n";
style += ".fy:visited {color: #333333;text-decoration: underline;font-size: 12px;} \n";
style += ".fy:hover{color: #000000;text-decoration: none;border: 1px solid #999999;";
style += "background-position: center center;font-size: 12px; background-color: #FFFFFF;} \n";
style += ".fy:active {color: #000000;text-decoration: none;border: 1px solid #999999;";
style += "background-position: center center;font-size: 12px;background-color: #FFFFFF;} \n";
style += "</style> \n";
String str = "";
int prev = intPage - 1;
int next = intPage + 1;
str = str + "<table width=100% border=0 cellspacing=0 cellpadding=1 class=fytd>";
str = str + "<tr><td width=45%>共计:<font color=#FF3300>[" + getCountic() +
"]</font>条 <font color=#FF3300> [" + getPageCount() + "]</font>页";
str = str + " 第<font color=#FF3300>[" + getIntPage() +
"]</font>页</td><td width=55%>";
str = str +
"<table width=275 border=0 align=right cellpadding=0 cellspacing=0 class=fytd>";
//*******
String pstr =HttpFile+"?"+PageParameter.replace("&pages=", "");
pstr =pstr.replace("?pages=", "");
str = str + "<form action="+pstr+" name=formin method=post><tr><td width=195>";
if (intPage > 1) {
str = str + " <A href=" + HttpFile + "?"+PageParameter+"1" +
" class=fy>[首页]</A> ";
} else {
str = str + " [首页] ";
}
if (intPage > 1) {
str = str + " <A href=" + HttpFile + "?"+PageParameter+prev +
" class=fy>[上一页]</A> ";
} else {
str = str + " [上一页] ";
}
if (intPage < intPageCount) {
str = str + " <A href=" + HttpFile + "?"+PageParameter+ next +
" class=fy>[下一页]</A> ";
} else {
str = str + " [下一页] ";
}
if (intPageCount > 1 && intPage != intPageCount) {
str = str + " <A href=" + HttpFile + "?"+PageParameter + intPageCount +
" class=fy>[尾页]</A>";
} else {
str = str + " [尾页]";
}
str = str +
"</td><td width=80 align=right><input name=pages type=text class=page value=" +
intPage +
" size=3 maxlength=5 onkeyup=javascript:value=value.replace(/[^\\d]/g,'')>";
str = str +
"<input name=Submit2 type=submit class=page value=转到></td></tr></form></table>";
return style+str;
}
//记录总数
public int getCountic() {
return intCountic;
}
//总页数
public int getPageCount() {
return intPageCount;
}
//当前页数
public int getIntPage() {
return intPage;
}
// 得到 汇总语句
public String getCountJsql(String sql, String DataKeyField) {
String[] str = sql.split("from");
没有合适的资源?快使用搜索试试~ 我知道了~
Java+jsp后台分页 oracle数据库
共14个文件
jsp:1个
classpath:1个
project:1个
4星 · 超过85%的资源 需积分: 9 54 下载量 24 浏览量
2009-03-25
18:11:48
上传
评论 2
收藏 1.09MB RAR 举报
温馨提示
这是我自己改的分页代码 呵呵 写了将近一个星期 只有一个控制类和一个jsp页 方便阅读和修改 只要修改test.java里面的数据库连接和表名就可以用了 希望大家工作和学习顺利 里面的class12.jar这个包是因为我的数据库有点小脾气 大家可以用自己的 也可以用我的 已经封装在包里的lib目录下了 需要改一下属性中的jar包路径
资源推荐
资源详情
资源评论
收起资源包目录
分页.rar (14个子文件)
分页
sql.dmp 6KB
.project 1KB
.settings
.jsdtscope 406B
org.eclipse.jdt.core.prefs 334B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 285B
WebRoot
WEB-INF
web.xml 375B
lib
classes12.jar 1.14MB
classes
com
hu
Test.class 9KB
index.jsp 922B
META-INF
MANIFEST.MF 39B
.myeclipse
src
com
hu
Test.java 9KB
.classpath 400B
共 14 条
- 1
demon_hu
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页