自己写的javabean分页呵呵

2024-04-25 11:58
文章标签 java 分页 bean 呵呵

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



http://www.chinasem.cn/article/934617

相关文章

Java实现将byte[]转换为File对象

《Java实现将byte[]转换为File对象》这篇文章将通过一个简单的例子为大家演示Java如何实现byte[]转换为File对象,并将其上传到外部服务器,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言1. 问题背景2. 环境准备3. 实现步骤3.1 从 URL 获取图片字节数据3.2 将字节数组

Java捕获ThreadPoolExecutor内部线程异常的四种方法

《Java捕获ThreadPoolExecutor内部线程异常的四种方法》这篇文章主要为大家详细介绍了Java捕获ThreadPoolExecutor内部线程异常的四种方法,文中的示例代码讲解详细,感... 目录方案 1方案 2方案 3方案 4结论方案 1使用 execute + try-catch 记录

SpringBoot接收JSON类型的参数方式

《SpringBoot接收JSON类型的参数方式》:本文主要介绍SpringBoot接收JSON类型的参数方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、jsON二、代码准备三、Apifox操作总结一、JSON在学习前端技术时,我们有讲到过JSON,而在

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g

Spring Security注解方式权限控制过程

《SpringSecurity注解方式权限控制过程》:本文主要介绍SpringSecurity注解方式权限控制过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、摘要二、实现步骤2.1 在配置类中添加权限注解的支持2.2 创建Controller类2.3 Us

Spring MVC跨域问题及解决

《SpringMVC跨域问题及解决》:本文主要介绍SpringMVC跨域问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录跨域问题不同的域同源策略解决方法1.CORS2.jsONP3.局部解决方案4.全局解决方法总结跨域问题不同的域协议、域名、端口

SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法

《SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法》本文主要介绍了SpringBoot项目启动错误:找不到或无法加载主类的几种解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录方法1:更改IDE配置方法2:在Eclipse中清理项目方法3:使用Maven命令行在开发Sprin

JAVA SE包装类和泛型详细介绍及说明方法

《JAVASE包装类和泛型详细介绍及说明方法》:本文主要介绍JAVASE包装类和泛型的相关资料,包括基本数据类型与包装类的对应关系,以及装箱和拆箱的概念,并重点讲解了自动装箱和自动拆箱的机制,文... 目录1. 包装类1.1 基本数据类型和对应的包装类1.2 装箱和拆箱1.3 自动装箱和自动拆箱2. 泛型2

SpringBoot操作MaxComputer方式(保姆级教程)

《SpringBoot操作MaxComputer方式(保姆级教程)》:本文主要介绍SpringBoot操作MaxComputer方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的... 目录引言uqNqjoe一、引入依赖二、配置文件 application.properties(信息用自己

JavaScript中的Map用法完全指南

《JavaScript中的Map用法完全指南》:本文主要介绍JavaScript中Map用法的相关资料,通过实例讲解了Map的创建、常用方法和迭代方式,还探讨了Map与对象的区别,并通过一个例子展... 目录引言1. 创建 Map2. Map 和对象的对比3. Map 的常用方法3.1 set(key, v