1.首先是DBcon类的代码,这个类就是连接数据用的类,具体的代码为:
package com.student.util.page;
import java.sql.*;
public class DBcon {
private String url = "jdbc:oracle:thin:@192.168.0.21:1521:orcl";
private String driver = "oracle.jdbc.driver.OracleDriver";
String login_name = "mianshi";
String pwd = "mianshi";
Connection con = null;
Statement stmt = null;
public DBcon() throws ClassNotFoundException, SQLException {
Class.forName(driver);
con = DriverManager.getConnection(url,login_name,pwd);
stmt = con.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
}
public Connection getCon() throws SQLException {
return con;
}
public void setCon(Connection con) {
this.con = con;
}
public Statement getStmt() {
return stmt;
}
public void setStmt(Statement stmt) {
this.stmt = stmt;
}
}
2.还要有一个bean,用来记录分页的总页数,记录总数,待显示页码.
package com.student.util.page;
public class PartBean {
int intPageSize = 5; // 一页显示的记录数
int intRowCount; // 记录总数
int intPageCount; // 总页数
int intPage = 1; // 待显示页码
String strPage;
public int getIntPage() {
return intPage;
}
public void setIntPage(int intPage) {
this.intPage = intPage;
}
public int getIntPageCount() {
return intPageCount;
}
public void setIntPageCount(int intPageCount) {
this.intPageCount = intPageCount;
}
public int getIntPageSize() {
return intPageSize;
}
public void setIntPageSize(int intPageSize) {
this.intPageSize = intPageSize;
}
public int getIntRowCount() {
return intRowCount;
}
public void setIntRowCount(int intRowCount) {
this.intRowCount = intRowCount;
}
public String getStrPage() {
return strPage;
}
public void setStrPage(String strPage) {
this.strPage = strPage;
}
}
3.用来显示的页面partpage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="db" class="com.student.util.page.DBcon" ></jsp:useBean>
<jsp:setProperty property="con" name="db"/>
<jsp:setProperty property="stmt" name="db"/>
<br> <br> <br>
<%
java.sql.ResultSet rs; //结果集对象
String sql; //SQL语句
int pageSize; //一页显示的记录数
int RowCount; //记录总数
int PageCount; //总页数
int intPage; //待显示页码
String strPage;
int i;
pageSize = 5;
strPage = request.getParameter("page");
if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据
intPage = 1;
}
else{
intPage = java.lang.Integer.parseInt(strPage);
if(intPage<1) intPage = 1;
}
//连接数据库
db.getCon().createStatement();
//创建一个可以滚动的只读的SQL语句对象
sql = "select * from personnel";
rs=db.getStmt().executeQuery(sql);
rs.last();
RowCount = rs.getRow();
PageCount = (RowCount+pageSize-1) / pageSize;
if(intPage>PageCount) intPage = PageCount;
%>
<body>
<table border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>出生日期</th>
</tr>
<%
if(PageCount>0){
rs.absolute((intPage-1) * pageSize + 1);
i = 0;
while(i<pageSize && !rs.isAfterLast()){
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
</tr>
<%
rs.next();
i++;
}
}
%>
</table>
<p align="center">
第 <input type="text" value="<%=intPage%>" size="1" style> 页
共
<font color="red" size=15>
<%=PageCount%></font> 页 <br>
<%if(intPage<PageCount){%>
<a href="partpage.jsp?page=<%=intPage+1%>">下一页</a>
<%}%>
<%if(intPage>1){%><a href="partpage.jsp?page=<%=intPage-1%>">上一页</a><%}%>
</p>
<%
//关闭结果集
rs.close();
//关闭SQL语句对象
db.getCon().close();
db.getStmt().close();
//关闭数据库
%>
以上就是,jsp+javabean+oracle 开发的分页的全部代码,呵呵!!
xh