------------------------------------------------------------------------------------------
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数据分行
a{ color:#666; text-decoration:none;}
a:link{color:#666; text-decoration:none;}
a:visited{color:#666; text-decoration:none; }
a:hover,a:active,a:focus{ color:#C00; text-decoration:none;
}
| ${emp.row2
} |
${emp.empno
} |
${emp.ename
} |
${emp.job
} |
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------