自己写的javabean分页呵呵

 
package my.data;
import java.sql.*;
public class Fy {
	
	Connection cn=null;
	Statement sql=null;
	ResultSet rs=null;
	
	String path=""; //数据库路径
	String username="";
	String userpass="";
	
	int recordcount=0;
	int pagecount=0;
	
	int p=1;
	int pagesize=10;
	
	
	public String getPath() {
		return path;
	}

	public void setPath(String path,String username,String userpass) {
		this.path = path;
		this.username=username;
		this.userpass=userpass;
	}

	public void setCn(Connection cn) {
		this.cn = cn;
	}
	
	public Fy()
	{
		
	}
	public void openDb()
	{
	try{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		cn=DriverManager.getConnection(path,username,userpass);
	}
	catch(Exception e)
	{
		System.err.print("error in openDb()");
	}
	}
	
	public ResultSet getRs(String sqlstr)
	{
		try
		{
			sql=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs=sql.executeQuery(sqlstr);
			
		}
		catch(Exception e)
		{
			System.err.print("error in getRs()");
			rs=null;
		}
		finally
		{
		return rs;
		}
	}
	public ResultSet getFyRs(String sqlstr,int p,int pagesize)
	{
		
		int postion;
		
		this.p=p;
		this.pagesize=pagesize;
		try
		{
			//取记录总数

			sql=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs=sql.executeQuery(sqlstr);
			rs.last();
			this.recordcount=rs.getRow();
			if(recordcount!=0)
			{
				this.pagecount=(recordcount%pagesize==0)?(recordcount/pagesize):(recordcount/pagesize+1);

				if(p<=1)
				{
				   p=1;
				}
				if(p>=pagecount)
				{
				   p=pagecount;
				}
				
				postion=(p-1)*pagesize+1;
				rs.absolute(postion);
			}
			else
			{
				System.err.print("no record");
				rs=null;
			}
			
		}
		catch(Exception e)
		{
			System.err.print("error in getRs()");
			rs=null;
		}
		finally
		{
		return rs;
		}
	}
	
	public void closeDB(){
	if(sql!=null)
		{
		try {
			sql.close();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		}
	if(cn!=null)
		{
		try{
			cn.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		}
	}
	
	public String showfy(String http)
	{
		StringBuffer sb=new StringBuffer();
		String t="";
		if(recordcount==0)
		{
			 t="数据库中没有记录!";
		}
		else
		{
		sb.append("共"+ recordcount +"条记录 ");
		sb.append(p+"/"+pagecount);
		sb.append(" 每页显示"+ pagesize +"条");
		t=new String(sb);
		}
				
		return t;
	}
	
}

 

 

调用

 

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<%@ include file="../admin/conn.jsp" %>
<%@ page import="my.data.Fy" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<%
ResultSet rs=null;
int a=0;

Fy ofy=new Fy();
ofy.setPath("jdbc:odbc:yangzhi","sa","abc");
ofy.openDb();
rs=ofy.getRs("select top 10 id,title,date from wz_list  order by id desc");
%>
<table>
<tr><td width=10%>编号</td><td width=70%>主题</td><td width=20%>时间</td></tr>
<%while(rs.next())
{ %>
<tr>
<td width=10%>
<%=rs.getInt("id") %></td>
<td width=70%><%=rs.getString("title") %></td>
<td width=20%><%=rs.getDate("date").toLocaleString() %></td></tr>
<%} %>
</table>
<br>
<%
ofy.closeDB();

ResultSet rs2=null;
ofy.openDb();
rs2=ofy.getFyRs("select  id,title,date from wz_list where id>1 order by id desc",2,1);

%>

<table>
<tr><td width=10%>编号</td><td width=70%>主题</td><td width=20%>时间</td></tr>
<%
if(rs2!=null){
int i=0;
while(i<1 && ! rs2.isAfterLast())
{ %>
<tr>
<td width=10%>
<%=rs2.getInt("id") %></td>
<td width=70%><%=rs2.getString("title") %></td>
<td width=20%><%=rs2.getDate("date").toLocaleString() %></td></tr>
<%
i=i+1;
rs2.next();
}
}%>
</table>
<%=ofy.showfy("") %>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值