java连接oracle_11g分页显示记录
记录学习的脚步
使用jdbc方式连接oracle数据库,然后分页显示scott用户的emp表中的记录
其中 让人比较费劲的就是oracle的分页查询 需要用到子查询和rownum
rownum是按记录的插入顺序生成的行编号,默认情况下为1,所以只有进行小于操作的时候,rownum才有意义
而对于rownum的详细详解 参看这位老兄写的 http://www.cnblogs.com/zjrstar/archive/2006/08/31/491090.html
index.jsp的代码
<%@page import="com.undergrowth.EmpBean"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="com.undergrowth.ConnDB"%> <%@page import="java.sql.Connection"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <% int pageNow=1; String s_pageNow=request.getParameter("pageNow"); if(s_pageNow!=null) pageNow=Integer.parseInt(s_pageNow); int pageSize=3; //每一页显示多少条记录 int rowCount=ConnDB.getRowCount(); //总共有多少行 int pageCount=0; //总共有多少页 //获取总共有多少页 if(rowCount%pageSize==0) { pageCount=rowCount/pageSize; }else { pageCount=rowCount/pageSize+1; } ArrayList<EmpBean> alBeans=ConnDB.getAllRecord(pageNow,pageSize); if(alBeans.size()>0){ out.print("<table align='center' border=1>"+ "<caption>雇员信息表</caption>"+"<tr><th>编号</th><th>姓名</th><th>工作</th><th>领导</th><th>雇佣日期</th><th>薪水</th><th>补贴</th><th>部门编号</th></tr>"); //显示记录 for(EmpBean bean:alBeans) { out.print("<tr>"); out.print("<td>"+bean.getEmpno()+"</td>"); out.print("<td>"+bean.getEname()+"</td>"); out.print("<td>"+bean.getJob()+"</td>"); out.print("<td>"+bean.getMgr()+"</td>"); out.print("<td>"+bean.getHiredate()+"</td>"); out.print("<td>"+bean.getSal()+"</td>"); out.print("<td>"+bean.getComm()+"</td>"); out.print("<td>"+bean.getDeptno()+"</td>"); out.print("</tr>"); } out.print("<tr align='center'>"); out.print("<td colspan=8>"); //显示上一页 if(pageNow!=1) out.print("[<a href=index.jsp?pageNow="+(pageNow-1)+">上一页</a>]"); for(int i=1;i<=pageCount;i++) { out.print("[<a href=index.jsp?pageNow="+i+">"+i+"</a>]"); } //显示下一页 if(pageNow!=pageCount) out.print("[<a href=index.jsp?pageNow="+(pageNow+1)+">下一页</a>]"); out.print("</td>"); out.print("</tr>"); out.print("</table>"); } %> </center> </body> </html>
用于连接数据库的ConnDB.java
package com.undergrowth; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; public class ConnDB { private static String driverName="oracle.jdbc.driver.OracleDriver"; private static String url="jdbc:oracle:thin:@localhost:1521:under"; private static String user="scott"; private static String password="scott"; private static Connection connection=null; private static PreparedStatement ps=null; private static ResultSet rs=null; public static Connection getConnection() { try { //注册驱动 Class.forName(driverName); //建立连接 connection=DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } return connection; } public static int getRowCount() { int rowCount=0; connection=getConnection(); //获取到员工表中有多少条记录 String sql="select count(*) from emp"; try { ps = connection.prepareStatement(sql); rs=ps.executeQuery(); if(rs.next()){ rowCount=Integer.parseInt(rs.getString(1)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeRes(rs, ps, connection); } return rowCount; } public static ArrayList<EmpBean> getAllRecord(int pageNow,int pageSize) { ArrayList<EmpBean> alBeans=new ArrayList<EmpBean>(); try { connection=getConnection(); //String sql="select * from emp where rownum<="+pageNow*pageSize + "and rownum>="+(pageNow-1)*pageSize+1; String sql="select a2.* from (select a1.*,rownum rn from (select * from emp) a1 where rownum<="+pageNow*pageSize+") a2 where a2.rn>="+((pageNow-1)*pageSize+1); //System.out.println(sql); ps=connection.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()) { EmpBean empBean=new EmpBean(); empBean.setEmpno(rs.getString(1)); empBean.setEname(rs.getString(2)); empBean.setJob(rs.getString(3)); empBean.setMgr(rs.getString(4)); empBean.setHiredate(rs.getDate(5)); empBean.setSal(rs.getFloat(6)); empBean.setComm(rs.getFloat(7)); empBean.setDeptno(rs.getString(8)); alBeans.add(empBean); } } catch (Exception e) { // TODO: handle exception } return alBeans; } public static void closeRes(ResultSet rs,PreparedStatement ps,Connection connection) { if(rs!=null) try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(ps!=null) try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(connection!=null) try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
辅助Bean EmpBean.java
package com.undergrowth; import java.util.Date; public class EmpBean { private String empno; public String getEmpno() { return empno; } public void setEmpno(String empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String getMgr() { return mgr; } public void setMgr(String mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Float getSal() { return sal; } public void setSal(Float sal) { this.sal = sal; } public Float getComm() { return comm; } public void setComm(Float comm) { this.comm = comm; } public String getDeptno() { return deptno; } public void setDeptno(String deptno) { this.deptno = deptno; } private String ename; private String job; private String mgr; private Date hiredate; private Float sal; private Float comm; private String deptno; }
测试效果
输入: http://localhost:8080/PagingOracle/
效果:
posted on 2013-09-22 16:40 liangxinzhi 阅读(285) 评论(0) 编辑 收藏 举报