本文主要是介绍自己写的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>
这篇关于自己写的javabean分页呵呵的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!