使用Jsp/Js/Ajax/Json/Jquery/Easyui + Servlet + JDBC + Lucene/Mysql/Oracle完成数据库分页
package loaderman.action; import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import loaderman.entity.Page;
import loaderman.service.impl.EmpService;
import loaderman.service.inter.IEmpService;
import net.sf.json.JSONArray; /**
* 员工管理模块
* 控制器
*/
public class EmpServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
this.doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
try {
//设置编号方式
request.setCharacterEncoding("UTF-8"); //获取客户端传入的参数
String strPage = request.getParameter("page");//rows
if(strPage == null || strPage.trim().length()==0){
strPage = "1";
}
Integer currPageNO = Integer.parseInt(strPage); //调用业务层
IEmpService iEmpService = new EmpService();
Page page = iEmpService.show(currPageNO); //创建Map集合
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("total",page.getAllRecordNO());
map.put("rows",page.getEmpList()); //使用第三方工具将map转成json文本
JSONArray jsonArray = JSONArray.fromObject(map);
String jsonJAVA = jsonArray.toString(); //去掉二边的空格
jsonJAVA = jsonJAVA.substring(1,jsonJAVA.length()-1);
System.out.println("jsonJAVA=" + jsonJAVA); //以字符流的方式,将json字符串输出到客户端
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
pw.write(jsonJAVA);
pw.flush();
pw.close(); }catch (Exception e) {
e.printStackTrace();
}
}
}
package loaderman.dao.impl; import java.math.BigDecimal;
import java.util.List; import loaderman.dao.inter.IEmpDao;
import loaderman.entity.Emp;
import loaderman.util.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler; /**
* 员工管理模块
* 持久层实现
*/
public class EmpDao implements IEmpDao{
public Integer getAllRecord() throws Exception {
QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
String sql = "SELECT COUNT(EMPNO) FROM EMP";
BigDecimal bigDecimal = (BigDecimal) runner.query(sql,new ScalarHandler());
return bigDecimal.intValue();
}
public List<Emp> findAllRecord(int start, int end) throws Exception {
QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
String sql = " SELECT XX.EMPNO,XX.ENAME,XX.JOB,XX.MGR,XX.HIREDATE,XX.SAL,XX.COMM,XX.DEPTNO" +
" FROM (SELECT ROWNUM ID,EMP.* FROM EMP WHERE ROWNUM<?) XX" +
" WHERE ID>?";
Object[] params = {end,start};
return runner.query(sql,new BeanListHandler<Emp>(Emp.class),params);
} public static void main(String[] args) throws Exception{
EmpDao dao = new EmpDao();
System.out.println("共有" + dao.getAllRecord() + "个员工");
System.out.println("------------------------------------------------第1页");
for(Emp e : dao.findAllRecord(0,4)){
System.out.println(e);
}
System.out.println("------------------------------------------------第2页");
for(Emp e : dao.findAllRecord(3,7)){
System.out.println(e);
}
System.out.println("------------------------------------------------第3页");
for(Emp e : dao.findAllRecord(6,10)){
System.out.println(e);
}
System.out.println("------------------------------------------------第4页");
for(Emp e : dao.findAllRecord(9,13)){
System.out.println(e);
}
System.out.println("------------------------------------------------第5页");
for(Emp e : dao.findAllRecord(12,16)){
System.out.println(e);
}
}
}
package loaderman.dao.inter; import loaderman.entity.Emp; import java.util.List; /**
* 员工管理模块
* 持久层接口
*/
public interface IEmpDao {
/**
* 获取总记录数
* @return 总记录数
*/
public Integer getAllRecord() throws Exception;
/**
* 分批查询所有记录
* @param start 表示 从第几条记录开始
* @param end 表示 到第几条记录结束
* @return 表示 start到end之间的记录集合,包含start和end
*/
public List<Emp> findAllRecord(int start,int end) throws Exception;
}
package loaderman.entity; import java.util.Date; /**
* 员工
*/
public class Emp {
private Integer empno;//编号
private String ename;//姓名
private String job;//工作
private Integer mgr;//上级编号
private Date hiredate;//入职时间
private Integer sal;//月薪
private Integer comm;//佣金
private Integer deptno;//部门编号
public Emp(){}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer 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 Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public Integer getSal() {
return sal;
}
public void setSal(Integer sal) {
this.sal = sal;
}
public Integer getComm() {
return comm;
}
public void setComm(Integer comm) {
this.comm = comm;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return this.empno+":"+this.ename+":"+this.sal+":"+this.hiredate+":"+this.deptno;
}
}
package loaderman.entity; import java.util.ArrayList;
import java.util.List; /**
* 分页类
* @author AdminTC
*/
public class Page {
private Integer currPageNO;//当前页号OK
private Integer perPageSize = 9;//每页显示记录数,默认为3条记录OK
private Integer allRecordNO;//总记录数OK
private Integer allPageNO;//总页号OK
private List<Emp> empList = new ArrayList<Emp>();//该本页显示的内容OK
public Page(){}
public Integer getCurrPageNO() {
return currPageNO;
}
public void setCurrPageNO(Integer currPageNO) {
this.currPageNO = currPageNO;
}
public Integer getPerPageSize() {
return perPageSize;
}
public void setPerPageSize(Integer perPageSize) {
this.perPageSize = perPageSize;
}
public Integer getAllRecordNO() {
return allRecordNO;
}
public void setAllRecordNO(Integer allRecordNO) {
this.allRecordNO = allRecordNO;
}
public Integer getAllPageNO() {
return allPageNO;
}
public void setAllPageNO(Integer allPageNO) {
this.allPageNO = allPageNO;
}
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
}
package loaderman.service.impl; import loaderman.dao.impl.EmpDao;
import loaderman.dao.inter.IEmpDao;
import loaderman.entity.Emp;
import loaderman.entity.Page;
import loaderman.service.inter.IEmpService; import java.util.List; /**
* 员工管理模块
* 业务层实现
*/
public class EmpService implements IEmpService{
private IEmpDao iEmpDao = new EmpDao();
public Page show(int currPageNO) throws Exception {
Page page = new Page(); //封装当前页号
page.setCurrPageNO(currPageNO); //封装总记录数
Integer allRecordNO = iEmpDao.getAllRecord();
page.setAllRecordNO(allRecordNO); //封装总页数
Integer allPageNO = null;
if(page.getAllRecordNO() % page.getPerPageSize() == 0){
allPageNO = page.getAllRecordNO() / page.getPerPageSize();
}else{
allPageNO = page.getAllRecordNO() / page.getPerPageSize() + 1;
}
page.setAllPageNO(allPageNO); //封装该本显示的内容
Integer start = (page.getCurrPageNO()-1) * page.getPerPageSize();
Integer end = page.getCurrPageNO() * page.getPerPageSize() + 1;
List<Emp> empList = iEmpDao.findAllRecord(start,end);
page.setEmpList(empList); return page;
} public static void main(String[] args) throws Exception{
EmpService service = new EmpService();
System.out.println("---------------------------------------NO1");
Page page = service.show(1);
for(Emp e : page.getEmpList()){
System.out.println(e);
}
System.out.println("---------------------------------------NO2");
page = service.show(2);
for(Emp e : page.getEmpList()){
System.out.println(e);
}
System.out.println("---------------------------------------NO3");
page = service.show(3);
for(Emp e : page.getEmpList()){
System.out.println(e);
}
System.out.println("---------------------------------------NO4");
page = service.show(4);
for(Emp e : page.getEmpList()){
System.out.println(e);
}
System.out.println("---------------------------------------NO5");
page = service.show(5);
for(Emp e : page.getEmpList()){
System.out.println(e);
}
}
}
package loaderman.service.inter; import loaderman.entity.Page;
/**
* 员工管理模块
* 业务层接口
*/
public interface IEmpService {
/**
* 根据页号获取该页需要显示的内容
* @param currPageNO 当前页号
* @return 封装该页需要显示的内容
*/
public Page show(int currPageNO) throws Exception;
}
package loaderman.util; import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 工具类
*/
public class JdbcUtil {
/**
* 加载src目录下的c3p0-config.xml文件
*/
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
/**
* 获取数据源
* @return 数据源
*/
public static ComboPooledDataSource getDataSource() {
return dataSource;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
<property name="user">scott</property>
<property name="password">tiger</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">5</property>
<property name="minPoolSize">1</property>
<property name="acquireIncrement">2</property>
</default-config>
</c3p0-config>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>EmpServlet</servlet-name>
<servlet-class>loaderman.action.EmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmpServlet</servlet-name>
<url-pattern>/EmpServlet</url-pattern>
</servlet-mapping> </web-app>
使用Jsp/Js/Ajax/Json/Jquery/Easyui + Servlet + JDBC + Lucene/Mysql/Oracle完成数据库分页的更多相关文章
- Jsp +Js + Jquery + EasyUI + Servlet + Lucene,完成分页
package loaderman.fy.action; import java.io.IOException; import java.io.PrintWriter; import java.uti ...
- Ajax json jquery实现菜单案例
需求: 运用AJAX请求文件menu.json,配置菜单栏,并实现以下功能点: 1. 点击向左箭头,菜单向左移动,隐藏 2. 点击向右箭头,菜单向右移动,显示3. 点击一级菜单,被点击菜单的子菜单显示 ...
- 使用Jsp +Js + Jquery + EasyUI + Servlet + Lucene,完成分页
步一:创建ArticleDao.java类 public class ArticleDao { public Integer getAllObjectNum(String keywords) thro ...
- php ajax json jquery 记录
php+jquery+ajax+json简单小例子 <html> <title>php+jquery+ajax+json简单小例子</title> <?php ...
- Ajax+json+jquery实现无限瀑布流布局
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- AJAX+json+jquery实现预加载瀑布流布局
宽度是一定的高度不定的瀑布流布局 也可以说是无缝拼图 当浏览器滚动到底部时候自动加载图片 加载的图片地址用json 在img.js里 ,还有正在加载动画是用 css3制作的 在ff等支持css3可以显 ...
- 原生js ajax与jquery ajax的区别
原生js ajax的调用: ajax({ type : "get", url : "02_ajax_get.txt", data : { "userN ...
- Maven+JSP+Servlet+JDBC+Redis+Mysql实现的黑马旅游网
项目简介 项目来源于:https://gitee.com/haoshunyu/travel 本系统是基于Maven+JSP+Servlet+JdbcTemplate+Redis+Mysql实现的旅游网 ...
- 24、jQuery常用AJAX-API/Java调用MySQL / Oracle过程与函数
1)掌握jQuery常用AJAX-API 2)掌握Java调用MySQL / Oracle过程与函数 一)jQuery常用AJAX-API 目的:简化客户端与服务端进行局部刷新的异步通讯 (1)取 ...
随机推荐
- 我理解的epoll(一)——实现分析
epoll项目中用了几次,但是对于其原理只是一知半解.我希望通过几篇blog能加深对她的理解. 我认为epoll是同步IO,因为他在调用epoll_wait时,内核在有I/O就绪前是阻塞的,虽然可以将 ...
- 10_Hive自定义函数UDF
Hive官方的UDF手册地址是:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1.使用内置函数的快捷方法: 创 ...
- django 发送邮件功能
setting.py # 邮件配置 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.e ...
- Python 获取 exe 的 icon 并且保存
Python 获取 exe 的 icon 并且保存 参考链接:https://mail.python.org/pipermail/python-win32/2009-April/009078.html ...
- string::cbegin string::cend
const_iterator cbegin() const noexcept; const_iterator cend() const noexcept;注:返回常量迭代器,不能修改 #include ...
- Vue入门(三)——模拟网络请求加载本地数据
1.首先我们需要在webpack.dev.conf.js中const PORT = process.env.PORT && Number(process.env.PORT) 的后面追加 ...
- 给你自己的博客加个 Markdown
今天给大家推荐一个简单易用的开源 Markdown 组件, Markdown 组件仓库地址是: https://github.com/pandao/editor.md Markdown 是什么 Mar ...
- Luogu P4781【模板】拉格朗日插值
洛谷传送门 板题-注意一下求多个数的乘积的逆元不要一个个快速幂求逆元,那样很慢,时间复杂度就是O(n2log)O(n^2log)O(n2log).直接先乘起来最后求一次逆元就行了.时间复杂度为O(nl ...
- Educational Codeforces Round 73 (Rated for Div. 2) A. 2048 Game
链接: https://codeforces.com/contest/1221/problem/A 题意: You are playing a variation of game 2048. Init ...
- 4、docker镜像:花卷结构、commit镜像
1.是什么 docker images 镜像是一种轻量级.可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码.运行时.库.环境变量和配置文件. ...