oracle脚本:drop table t_student cascade constraints;
/*==============================================================*/
/* Table: t_student */
/*==============================================================*/
create table t_student (
s_id char(10) not null,
s_name varchar2(20) not null,
s_age char(2) not null,
s_sex char(2) not null,
s_class varchar2(20),
constraint PK_T_STUDENT primary key (s_id)
);
insert into t_student values('0001','张三','20','男','08级二班') ;
insert into t_student values('0002','李四','21','女','08级二班') ;
insert into t_student values('0003','王五','20','男','08级二班') ;
insert into t_student values('0004','赵柳','20','女','08级一班') ;
insert into t_student values('0005','杨梅','21','男','08级二班') ;
insert into t_student values('0006','刘海','23','女','08级一班') ;
insert into t_student values('0007','孙江','20','女','08级一班') ;
insert into t_student values('0008','苏灿','22','男','08级二班') ;
insert into t_student values('0009','王霞','23','女','08级一班') ;
insert into t_student values('0010','王猛','22','男','08级二班') ;
insert into t_student values('0011','张相','22','女','08级一班') ;
insert into t_student values('0012','香橙','20','女','08级一班') ;
insert into t_student values('0013','李心','21','女','08级二班') ;
insert into t_student values('0014','张强','20','男','08级一班') ;
insert into t_student values('0015','赵琳','21','女','08级一班') ;
insert into t_student values('0016','刘达','21','男','08级二班') ;
insert into t_student values('0017','苏惠','20','女','08级二班') ;
insert into t_student values('0018','贾瑞','20','女','08级一班') ;
insert into t_student values('0019','谷瑞坤','22','男','08级二班') ;
insert into t_student values('0020','祥还','21','男','08级一班') ;
commit;
/* Table: t_student */
/*==============================================================*/
create table t_student (
s_id char(10) not null,
s_name varchar2(20) not null,
s_age char(2) not null,
s_sex char(2) not null,
s_class varchar2(20),
constraint PK_T_STUDENT primary key (s_id)
);
insert into t_student values('0001','张三','20','男','08级二班') ;
insert into t_student values('0002','李四','21','女','08级二班') ;
insert into t_student values('0003','王五','20','男','08级二班') ;
insert into t_student values('0004','赵柳','20','女','08级一班') ;
insert into t_student values('0005','杨梅','21','男','08级二班') ;
insert into t_student values('0006','刘海','23','女','08级一班') ;
insert into t_student values('0007','孙江','20','女','08级一班') ;
insert into t_student values('0008','苏灿','22','男','08级二班') ;
insert into t_student values('0009','王霞','23','女','08级一班') ;
insert into t_student values('0010','王猛','22','男','08级二班') ;
insert into t_student values('0011','张相','22','女','08级一班') ;
insert into t_student values('0012','香橙','20','女','08级一班') ;
insert into t_student values('0013','李心','21','女','08级二班') ;
insert into t_student values('0014','张强','20','男','08级一班') ;
insert into t_student values('0015','赵琳','21','女','08级一班') ;
insert into t_student values('0016','刘达','21','男','08级二班') ;
insert into t_student values('0017','苏惠','20','女','08级二班') ;
insert into t_student values('0018','贾瑞','20','女','08级一班') ;
insert into t_student values('0019','谷瑞坤','22','男','08级二班') ;
insert into t_student values('0020','祥还','21','男','08级一班') ;
commit;
采用单利模式创建DbUtil类获得Connection对象:package com.stmcc.test.util;
import java.sql.*;
public class DbUtil {
private static final String driver = "oracle.jdbc.driver.OracleDriver" ;
private static final String url = "jdbc:oracle:thin:@10.10.10.2:1521:orcl" ;
private static final String username = "test" ;
private static final String password = "test" ;
public static Connection getConnection(){
Connection conn = null ;
try{
Class.forName(driver) ;
conn = DriverManager.getConnection(url, username, password) ;
}catch(Exception e){
e.printStackTrace() ;
}
return conn ;
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs ) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println(DbUtil.getConnection());
}
}
public class DbUtil {
private static final String driver = "oracle.jdbc.driver.OracleDriver" ;
private static final String url = "jdbc:oracle:thin:@10.10.10.2:1521:orcl" ;
private static final String username = "test" ;
private static final String password = "test" ;
public static Connection getConnection(){
Connection conn = null ;
try{
Class.forName(driver) ;
conn = DriverManager.getConnection(url, username, password) ;
}catch(Exception e){
e.printStackTrace() ;
}
return conn ;
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs ) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println(DbUtil.getConnection());
}
}
创建学生实体类:package com.stmcc.test;
public class Student {
private String s_id ;
private String s_name ;
private String s_age ;
private String s_sex ;
private String s_class ;
public String getS_id() {
return s_id;
}
public void setS_id(String s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
public String getS_age() {
return s_age;
}
public void setS_age(String s_age) {
this.s_age = s_age;
}
public String getS_sex() {
return s_sex;
}
public void setS_sex(String s_sex) {
this.s_sex = s_sex;
}
public String getS_class() {
return s_class;
}
public void setS_class(String s_class) {
this.s_class = s_class;
}
}
private String s_id ;
private String s_name ;
private String s_age ;
private String s_sex ;
private String s_class ;
public String getS_id() {
return s_id;
}
public void setS_id(String s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
public String getS_age() {
return s_age;
}
public void setS_age(String s_age) {
this.s_age = s_age;
}
public String getS_sex() {
return s_sex;
}
public void setS_sex(String s_sex) {
this.s_sex = s_sex;
}
public String getS_class() {
return s_class;
}
public void setS_class(String s_class) {
this.s_class = s_class;
}
}
创建学生管理类:package com.stmcc.test.util;
import com.stmcc.test.*;
import java.sql.* ;
import java.util.*;
public class StuManager {
private static StuManager instance = new StuManager() ;
private StuManager(){} ;
public static StuManager getInstance(){
return instance ;
}
// oracle实现分页的查询语句
// select s_id, s_name, s_age, s_sex, s_class
// from
// (
// select rownum rn, s_id, s_name, s_age, s_sex, s_class
// from
// (select s_id, s_name, s_age, s_sex, s_class
// from t_student order by s_id
// )where rownum <= 10
// )where rn > 5 ;
public PageModel findStudentList(int pageNo, int pageSize){
PageModel pageModel = null ;
StringBuffer sql = new StringBuffer() ;
sql.append("select s_id, s_name, s_age, s_sex, s_class ")
.append("from")
.append("(")
.append("select rownum rn, s_id, s_name, s_age, s_sex, s_class ")
.append("from")
.append("(")
.append("select s_id, s_name, s_age, s_sex, s_class ")
.append("from t_student order by s_id")
.append(")")
.append("where rownum <= ?")
.append(")")
.append("where rn > ? ");
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
try{
conn = DbUtil.getConnection() ;
pstmt = conn.prepareStatement(sql.toString()) ;
pstmt.setInt(1, pageNo*pageSize) ;
pstmt.setInt(2, (pageNo - 1)*pageSize) ;
rs = pstmt.executeQuery() ;
List<Student> stuList = new ArrayList<Student>() ;
while (rs.next()){
Student stu = new Student() ;
stu.setS_id(rs.getString("s_id")) ;
stu.setS_name(rs.getString("s_name")) ;
stu.setS_age(rs.getString("s_age")) ;
stu.setS_sex(rs.getString("s_sex")) ;
stu.setS_class(rs.getString("s_class")) ;
stuList.add(stu) ;
}
pageModel = new PageModel() ;
pageModel.setList(stuList) ;
pageModel.setTotalRecords(getTotalRecords(conn)) ;
pageModel.setPageSize(pageSize);
pageModel.setPageNo(pageNo);
}catch(Exception e){
e.printStackTrace() ;
}finally{
DbUtil.close(rs) ;
DbUtil.close(pstmt) ;
DbUtil.close(conn) ;
}
return pageModel ;
}
/**
* 取得总记录数
* @param conn
* @return
*/
private int getTotalRecords(Connection conn)
throws SQLException {
String sql = "select count(*) from t_student";
PreparedStatement pstmt = null;
ResultSet rs = null;
int count = 0;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
count = rs.getInt(1);
}finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
}
return count;
}
}
import java.sql.* ;
import java.util.*;
public class StuManager {
private static StuManager instance = new StuManager() ;
private StuManager(){} ;
public static StuManager getInstance(){
return instance ;
}
// oracle实现分页的查询语句
// select s_id, s_name, s_age, s_sex, s_class
// from
// (
// select rownum rn, s_id, s_name, s_age, s_sex, s_class
// from
// (select s_id, s_name, s_age, s_sex, s_class
// from t_student order by s_id
// )where rownum <= 10
// )where rn > 5 ;
public PageModel findStudentList(int pageNo, int pageSize){
PageModel pageModel = null ;
StringBuffer sql = new StringBuffer() ;
sql.append("select s_id, s_name, s_age, s_sex, s_class ")
.append("from")
.append("(")
.append("select rownum rn, s_id, s_name, s_age, s_sex, s_class ")
.append("from")
.append("(")
.append("select s_id, s_name, s_age, s_sex, s_class ")
.append("from t_student order by s_id")
.append(")")
.append("where rownum <= ?")
.append(")")
.append("where rn > ? ");
Connection conn = null ;
PreparedStatement pstmt = null ;
ResultSet rs = null ;
try{
conn = DbUtil.getConnection() ;
pstmt = conn.prepareStatement(sql.toString()) ;
pstmt.setInt(1, pageNo*pageSize) ;
pstmt.setInt(2, (pageNo - 1)*pageSize) ;
rs = pstmt.executeQuery() ;
List<Student> stuList = new ArrayList<Student>() ;
while (rs.next()){
Student stu = new Student() ;
stu.setS_id(rs.getString("s_id")) ;
stu.setS_name(rs.getString("s_name")) ;
stu.setS_age(rs.getString("s_age")) ;
stu.setS_sex(rs.getString("s_sex")) ;
stu.setS_class(rs.getString("s_class")) ;
stuList.add(stu) ;
}
pageModel = new PageModel() ;
pageModel.setList(stuList) ;
pageModel.setTotalRecords(getTotalRecords(conn)) ;
pageModel.setPageSize(pageSize);
pageModel.setPageNo(pageNo);
}catch(Exception e){
e.printStackTrace() ;
}finally{
DbUtil.close(rs) ;
DbUtil.close(pstmt) ;
DbUtil.close(conn) ;
}
return pageModel ;
}
/**
* 取得总记录数
* @param conn
* @return
*/
private int getTotalRecords(Connection conn)
throws SQLException {
String sql = "select count(*) from t_student";
PreparedStatement pstmt = null;
ResultSet rs = null;
int count = 0;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();
count = rs.getInt(1);
}finally {
DbUtil.close(rs);
DbUtil.close(pstmt);
}
return count;
}
}
创建分页模型类实现业务逻辑:package com.stmcc.test.util;
import java.util.* ;
public class PageModel {
//结果集
private List list ;
//查询总记录数
private int totalRecords ;
//每页多少条数据
private int pageSize ;
//第几页
private int pageNo ;
/**
* 总页数
* @return
*/
public int getTotalPages(){
return (totalRecords + pageSize -1) / pageSize ;
}
/**
* 取得首页
* @return
*/
public int getTopPageNo(){
return 1 ;
}
/**
* 上一页
* @return
*/
public int getPreviousPageNo(){
if(pageNo <= 1){
return 1 ;
}
return pageNo - 1 ;
}
/**
* 下一页
* @return
*/
public int getNextPageNo(){
if(pageNo >= getBottomPageNo()){
return getBottomPageNo() ;
}
return pageNo + 1 ;
}
/**
* 取得尾页
* @return
*/
public int getBottomPageNo(){
return getTotalPages() ;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
public class PageModel {
//结果集
private List list ;
//查询总记录数
private int totalRecords ;
//每页多少条数据
private int pageSize ;
//第几页
private int pageNo ;
/**
* 总页数
* @return
*/
public int getTotalPages(){
return (totalRecords + pageSize -1) / pageSize ;
}
/**
* 取得首页
* @return
*/
public int getTopPageNo(){
return 1 ;
}
/**
* 上一页
* @return
*/
public int getPreviousPageNo(){
if(pageNo <= 1){
return 1 ;
}
return pageNo - 1 ;
}
/**
* 下一页
* @return
*/
public int getNextPageNo(){
if(pageNo >= getBottomPageNo()){
return getBottomPageNo() ;
}
return pageNo + 1 ;
}
/**
* 取得尾页
* @return
*/
public int getBottomPageNo(){
return getTotalPages() ;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
jsp页面:<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="com.stmcc.test.util.*"%>
<%@ page import="com.stmcc.test.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<html>
<head><title>分页展示</title></head>
<%
int pageNo = 1 ;
int pageSize = 5 ;
String pageNoString = request.getParameter("pageNo") ;
if(pageNoString != null){
pageNo = Integer.parseInt(pageNoString) ;
}
PageModel pageModel = StuManager.getInstance().findStudentList(pageNo,pageSize) ;
%>
<script type="text/javaScript">
function topPage() {
window.self.location = "student.jsp?pageNo=<%=pageModel.getTopPageNo()%>";
}
function previousPage() {
window.self.location = "student.jsp?pageNo=<%=pageModel.getPreviousPageNo()%>";
}
function nextPage() {
window.self.location = "student.jsp?pageNo=<%=pageModel.getNextPageNo()%>";
}
function bottomPage() {
window.self.location = "student.jsp?pageNo=<%=pageModel.getBottomPageNo()%>";
}
</script>
<body>
<center>
<table border="1">
<tr>
<td>学生编号</td>
<td>学生姓名</td>
<td>学生年龄</td>
<td>学生性别</td>
<td>学生班级</td>
</tr>
<%
List stuList = pageModel.getList() ;
for(Iterator<Student> iter = stuList.iterator(); iter.hasNext();){
Student stu = iter.next() ;
%>
<tr>
<td><%=stu.getS_id() %></td>
<td><%=stu.getS_name() %></td>
<td><%=stu.getS_age() %></td>
<td><%=stu.getS_sex() %></td>
<td><%=stu.getS_class() %></td>
</tr>
<%
}
%>
<tr><td colspan="5">
共 <%=pageModel.getTotalPages() %> 页
当前第 <%=pageModel.getPageNo() %>页
<input type="button" value="首页" onClick="topPage()">
<input type="button" value="上一页" onClick="previousPage()">
<input type="button" value="下一页" onClick="nextPage()">
<input type="button" value="尾页" onClick="bottomPage()">
</td></tr>
</table>
</center>
</body>
</html>
<%@ page import="com.stmcc.test.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<html>
<head><title>分页展示</title></head>
<%
int pageNo = 1 ;
int pageSize = 5 ;
String pageNoString = request.getParameter("pageNo") ;
if(pageNoString != null){
pageNo = Integer.parseInt(pageNoString) ;
}
PageModel pageModel = StuManager.getInstance().findStudentList(pageNo,pageSize) ;
%>
<script type="text/javaScript">
function topPage() {
window.self.location = "student.jsp?pageNo=<%=pageModel.getTopPageNo()%>";
}
function previousPage() {
window.self.location = "student.jsp?pageNo=<%=pageModel.getPreviousPageNo()%>";
}
function nextPage() {
window.self.location = "student.jsp?pageNo=<%=pageModel.getNextPageNo()%>";
}
function bottomPage() {
window.self.location = "student.jsp?pageNo=<%=pageModel.getBottomPageNo()%>";
}
</script>
<body>
<center>
<table border="1">
<tr>
<td>学生编号</td>
<td>学生姓名</td>
<td>学生年龄</td>
<td>学生性别</td>
<td>学生班级</td>
</tr>
<%
List stuList = pageModel.getList() ;
for(Iterator<Student> iter = stuList.iterator(); iter.hasNext();){
Student stu = iter.next() ;
%>
<tr>
<td><%=stu.getS_id() %></td>
<td><%=stu.getS_name() %></td>
<td><%=stu.getS_age() %></td>
<td><%=stu.getS_sex() %></td>
<td><%=stu.getS_class() %></td>
</tr>
<%
}
%>
<tr><td colspan="5">
共 <%=pageModel.getTotalPages() %> 页
当前第 <%=pageModel.getPageNo() %>页
<input type="button" value="首页" onClick="topPage()">
<input type="button" value="上一页" onClick="previousPage()">
<input type="button" value="下一页" onClick="nextPage()">
<input type="button" value="尾页" onClick="bottomPage()">
</td></tr>
</table>
</center>
</body>
</html>
相关推荐
在本文中,我们将探讨如何使用JSP和Oracle数据库来实现一个简单的分页功能。分页是Web应用程序中常见的需求,特别是在处理大量数据时,它能够提高用户体验,避免一次性加载过多数据导致页面响应变慢。 首先,我们...
带学员课上做的oracle模仿百度分页样式的例子。比较适合初学者参考,jdbc工具类是自己写的,没有使用第三方分页组件。tomcat是apache-tomcat-7.0.41,myeclipse是MyEclipse 9.0,oracle是11g。例子中用的是海量数据...
总结起来,本示例通过JSP和Oracle数据库的结合,展示了如何在Web应用中实现分页功能,对初学者而言是一次宝贵的实践机会,能帮助他们更好地理解和运用相关技术。通过深入学习和实践这些例子,开发者可以逐步掌握分页...
本教程将详细讲解如何利用JSP(JavaServer Pages)、JavaBean和Servlet技术来实现数据分页功能。我们将讨论以下几个关键知识点: 1. **JSP基础**:JSP是Java的一种动态网页技术,它允许我们在HTML页面中嵌入Java...
项目中的"pagination_4_Oracle"可能包含了Oracle分页的具体实现。 5. MySQL数据库分页:MySQL提供了LIMIT关键字,可以直接配合OFFSET实现分页查询。在Java中,通过调整LIMIT子句的参数即可实现不同页码的数据获取。...
在“ssh+oracle分页”这个主题中,我们将深入探讨如何利用这三个框架与Oracle数据库进行集成,并实现数据的分页显示。 1. **Struts框架**:Struts是一个基于MVC(Model-View-Controller)设计模式的Java Web框架。...
本项目是基于`jsp`(Java Server Pages)和`Hibernate`框架实现的分页功能,通过Java代码来详细展示了如何在Web应用中高效地实现这一功能。 `jsp`是Java的一种动态网页技术,允许开发者在HTML中嵌入Java代码,以...
本文档介绍了一种基于JSP(Java Server Pages)结合Oracle数据库实现的数据分页显示方法。此方法利用了JDBC(Java Database Connectivity)2.0标准进行数据库连接与查询,并通过简单的逻辑实现了网页上的数据分页...
在这个场景下,"SSH+ORACLE好用分页"意味着我们将SSH框架与Oracle数据库结合,实现了高效的数据分页功能。 数据分页是Web应用中常见的需求,特别是在处理大量数据时,分页可以提高用户体验,避免一次性加载过多数据...
需要注意的是,这样的分页组件可能更适合简单的JSP+JDBC操作或简单的MVC架构。在更复杂的系统中,可能需要结合后端框架如Spring MVC、Struts等,以及前端库如Bootstrap、jQuery等来实现更强大和灵活的分页功能。 ...
本主题聚焦于使用JSP和JDBC在Oracle数据库上实现真正的分页功能。在Web开发中,分页是提高用户体验的关键技术,尤其是在处理大量数据时,它能让用户更有效地浏览和查找所需信息。这里我们将深入探讨如何结合Java ...
例如,以下是一个简单的分页查询示例: ```sql SELECT * FROM ( SELECT t.*, ROWNUM rn FROM YourTable t ORDER BY SomeColumn ) WHERE rn BETWEEN (your_page_number - 1) * your_rows_per_page + 1 AND your_...
综上所述,"java+jsp+servlet+dao+oracle做得分页 增删改查操作"是一个典型的Java Web应用开发流程,涉及了前端展示、后端处理、数据库交互以及分页技术。理解这些技术的原理和协作方式,对于理解和开发类似的Web...
本篇将详细介绍如何在JSP(JavaServer Pages)环境中,结合Oracle数据库实现分页功能,并通过一个名为"jsp分页 oracle 插件demo"的示例来演示具体步骤。 首先,我们需要了解JSP分页的基本原理。在JSP中,分页通常...
分页技术。实验一(1) Developer使用说明(1) 数据库管理(1) PL/SQL(1) Oracle客户端安装与配置(1) Oracle(1) sqlplus和isqlplus基本命令(1) 语言访问数据库(1) 用SQL(1)
【标题】:“影院信息查询(java+jsp+hibernate+dwr框架+分页技术+ajax+oracle)”是一个综合性的Web应用项目,它利用了一系列先进的技术和工具来实现对影院信息的高效、交互式查询。这个项目的核心在于,它展示了...
根据提供的信息,我们可以详细探讨如何使用 Oracle 数据库与 JSP(JavaServer Pages)技术结合来实现数据分页功能。在 Web 开发中,分页是非常常见的需求之一,它能够有效地提高用户体验并减轻服务器负担。 ### ...
因此,"JSP+JDBC_假分页"这个主题是关于如何在Java服务器页面(JSP)上使用Java数据库连接(JDBC)实现模拟分页效果的技术实践。 JSP(JavaServer Pages)是一种基于Java技术的动态网页开发工具,允许开发者在HTML...
实现【JSP+Oracle留言板】的步骤大致如下: 1. 数据库设计:创建一个名为`MESSAGE`的表,包含字段如`ID`(主键)、`USERNAME`、`CONTENT`、`DATE`等。 2. 编写JSP页面:创建一个`leave_message.jsp`用于显示留言表单...
总结来说,Oracle和JSP结合实现分页涉及以下几个步骤:在Oracle中构造分页查询,使用JDBC在Servlet中执行查询,然后在JSP页面中展示数据。理解并熟练掌握这些步骤对于开发高效、用户友好的数据驱动网站至关重要。在...