数据库连接通用类
package demo.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public abstract class BaseDao {
private final String user = "scott";
private final String pass = "tiger";
private final String url = "jdbc:oracle:thin:@192.168.90.38:1521:orcl";
public ResultSet rs = null;
public PreparedStatement pstm = null;
public Connection conn = null;
public Connection getConn() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, pass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void close() {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(pstm != null) {
pstm.close();
pstm = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
实体类:Emp
package demo.po;
import java.io.Serializable;
public class Emp implements Serializable{
private static final long serialVersionUID = 1L;
private int empno;
private String ename;
public Emp( int empno, String ename) {
this.empno = empno;
this.ename = ename;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}
**dao层接口:EmpDao **
package demo.dao;
import java.util.List;
import demo.po.Emp;
public interface EmpDao {
public List<Emp> findEmps(int start,int end);
public int findCount();
}
dao层实现类:EmpDaoImpl
package demo.dao.impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import demo.dao.BaseDao;
import demo.dao.EmpDao;
import demo.po.Emp;
public class EmpDaoImpl extends BaseDao implements EmpDao{
@Override
public List<Emp> findEmps(int start, int end) {
// TODO Auto-generated method stub
List<Emp> list = new ArrayList<Emp>();
try {
String sql = "select empno,ename from (select empno,ename,rownum r from emp ) e "
+ "where e.r between ? and ?";
conn= getConn();
pstm = conn.prepareStatement(sql);
pstm.setInt(1, start);
pstm.setInt(2, end);
rs = pstm.executeQuery();
while(rs.next()) {
Emp emp = new Emp(rs.getInt("empno"), rs.getString("ename"));
list.add(emp);
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return list;
}
@Override
public int findCount() {
// TODO Auto-generated method stub
int count = 0;
try {
String sql = "select count(1) c from emp";
conn= getConn();
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while(rs.next()) {
count = rs.getInt("c");
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
return count;
}
}
Service层的接口:EmpService
package demo.service;
import java.util.List;
import demo.po.Emp;
public interface EmpService {
public List<Emp> findEmps(int start,int end);
public int findCount();
public int findPageCount(int step);
}
Service层的实现类:EmpServiceImpl
package demo.service.impl;
import java.util.List;
import demo.dao.EmpDao;
import demo.dao.UserDao;
import demo.dao.impl.EmpDaoImpl;
import demo.dao.impl.UserDaoImpl;
import demo.po.Emp;
import demo.po.User;
import demo.service.EmpService;
import demo.service.UserService;
public class EmpServiceImpl implements EmpService{
private EmpDao empDao = new EmpDaoImpl();
@Override
public List<Emp> findEmps(int start, int end) {
// TODO Auto-generated method stub
return empDao.findEmps(start, end);
}
@Override
public int findCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int findPageCount(int step) {
// TODO Auto-generated method stub
int pageCount = 0;
int totalNum = empDao.findCount(); //一共有多少条记录
if(totalNum % step == 0) {
pageCount = totalNum/step;
}else {
pageCount = totalNum/step+1;
}
return pageCount;
}
}
jsp页面
list.jsp
<%@page import="demo.po.Emp"%>
<%@page import="java.util.List"%>
<%@page import="demo.service.impl.EmpServiceImpl"%>
<%@page import="demo.service.EmpService"%>
<%@page import="demo.po.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
th{
width:200px;
height: 100px;
background-color: gray;
}
td,th{
border: solid 1px gray;
text-align: center;
}
a{
margin-left: 10px;
}
</style>
</head>
<body>
<%
List<Emp> emps = (List)session.getAttribute("emps");
int total = (int)session.getAttribute("total"); //一共多少页
int num = (int)session.getAttribute("num"); //当前页
%>
<table>
<tr>
<th>员工编号</th>
<th>员工姓名</th>
</tr>
<%
for(int i =0;i<emps.size();i++){
Emp e = emps.get(i);
if(i%2==0){
%>
<tr style="background-color: skyblue">
<td><%=e.getEmpno() %></td>
<td><%=e.getEname() %></td>
</tr>
<%
}else{
%>
<tr>
<td><%=e.getEmpno() %></td>
<td><%=e.getEname() %></td>
</tr>
<%
}
%>
<%
}
int pre = num-1<=0 ? 1:num-1;
int next = num+1>=total ? total:num+1;
%>
<tr>
<td colspan="2"><a href="listc.jsp?pg=1">首页</a><a href="listc.jsp?pg=<%=pre%>">上一页</a><a href="listc.jsp?pg=<%=next%>">下一页</a><a href="listc.jsp?pg=<%=total%>">末页</a></td>
</tr>
</table>
</body>
</html>
listc.jsp
<%@page import="demo.po.Emp"%>
<%@page import="java.util.List"%>
<%@page import="demo.service.impl.EmpServiceImpl"%>
<%@page import="demo.service.EmpService"%>
<%@page import="demo.po.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//当前是第几页
int num =1; //当前页数
String pg = request.getParameter("pg");
if(pg != null){
num = Integer.parseInt(pg);
}
//每页显示多少条
int step = 5;
//查询分页
EmpService empService = new EmpServiceImpl();
int totalCount= empService.findPageCount(step);// 一共有多少页
int start = (num-1)*step+1;
int end = num*step;
List<Emp> emps = empService.findEmps(start, end);
session.setAttribute("emps", emps);
session.setAttribute("total",totalCount);
session.setAttribute("num",num);
response.sendRedirect("list.jsp");
%>