ITPub博客

首页 > 数据库 > Oracle > jsp+oracle分页

jsp+oracle分页

Oracle 作者:1980wang 时间:2013-12-09 12:43:39 0 删除 编辑
------------------------------------------------------------------------------------------
package com.bean;

public class EmpBean {

private String row2;
private String empno;
private String ename;
private String job;

public EmpBean() {
}

public EmpBean(String row2, String empno, String ename, String job) {
super();
this.row2 = row2;
this.empno = empno;
this.ename = ename;
this.job = job;
}

public String getRow2() {
return row2;
}

public void setRow2(String row2) {
this.row2 = row2;
}

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;
}

}

------------------------------------------------------------------------------------------
package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BaseDao {

static {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
}
}

public static Connection getConnection() {
Connection con = null;
try {
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");
} catch (Exception e) {
}

return con;
}

public static void closeAll(ResultSet rs, PreparedStatement pstm,
Connection con) {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (pstm != null) {
pstm.close();
pstm = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
}
}

public static void closeAll(PreparedStatement pstm, Connection con) {
try {

if (pstm != null) {
pstm.close();
pstm = null;
}
if (con != null) {
con.close();
con = null;
}
} catch (Exception e) {
}
}

}

------------------------------------------------------------------------------------------
package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.bean.EmpBean;


public class EmpDao {

// 取得所需要的数据
public static ArrayList getEmp(int start, int end) {
ArrayList list = new ArrayList();

Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
con = BaseDao.getConnection();
pstm = con
.prepareStatement("select row2, a3.empno , a3.ename, a3.job from "
+ "(select rownum row2 ,a2.* from (select emp.* from emp order by sal) a2) a3 "
+ "where row2 >? and row2 <= ? ");
pstm.setInt(1, start);
pstm.setInt(2, end);
rs = pstm.executeQuery();

while (rs.next()) {
EmpBean bean = bean = new EmpBean();
bean.setRow2(rs.getString(1));
bean.setEmpno(rs.getString(2));
bean.setEname(rs.getString(3));
bean.setJob(rs.getString(4));
list.add(bean);
}
} catch (Exception e) {
e.printStackTrace();
}

BaseDao.closeAll(rs, pstm, con);
return list;
}

// 计算查询数据多少行
public static int getCount() {
int count = 0;

Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
con = BaseDao.getConnection();
pstm = con.prepareStatement("select count(*) from emp ");
rs = pstm.executeQuery();
while (rs.next()) {
count = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}

BaseDao.closeAll(rs, pstm, con);
return count;
}

}

------------------------------------------------------------------------------------------
package com.service;

import java.util.ArrayList;

import com.bean.EmpBean;
import com.dao.EmpDao;

public class EmpService {

// 返回页数
public int getPages(int controlCount) {
int count = new EmpDao().getCount();
// 这里的算法是:算出总共多少条数据,如果条数
int pagesCount = count % controlCount == 0 ? count / controlCount
: count / controlCount + 1;
return pagesCount;
}

// 获得页面中要显示数据
public ArrayList getEmpList(int start,int controlCount) {
ArrayList list = null;
list = new EmpDao().getEmp(start, start + controlCount);
return list;
}

}

------------------------------------------------------------------------------------------
package com.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.EmpBean;
import com.service.EmpService;

public class EmpServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
EmpService empService = new EmpService();
// controlCount这个值代表了你每页显示的多少条数据
int controlCount = 2;
// 计算页数
int pageCount = empService.getPages(controlCount);

// 默认传过去是1
int start = 1;

// 从客户端拿值过来
String pageStart = request.getParameter("start");
if (pageStart != null) {
int temp = Integer.parseInt(pageStart);

if (temp == 0) {
temp += 1;
}

if (temp > pageCount) {
temp -= 1;
}

start = temp;
}

ArrayList list = empService.getEmpList((start - 1)
* controlCount, controlCount);

// 处理完了将上面值返回给jsp页面
request.setAttribute("emplist", list);// 带有数据的集合
request.setAttribute("pageCount", pageCount);// 共有多少页
request.setAttribute("start", start);

request.getRequestDispatcher("/index.jsp").forward(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}

------------------------------------------------------------------------------------------
<%@ page import="com.bean.EmpBean"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

emp数据分行



序号 工号 姓名 工种
${emp.row2 } ${emp.empno } ${emp.ename } ${emp.job }



------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------
<!-- 正文结束 -->

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23666173/viewspace-1117086/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-04-05