当前位置:网站首页>Paging query, using jdbc-- paging query
Paging query, using jdbc-- paging query
2022-06-30 04:31:00 【A fat man】
package com.htf.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.htf.bean.Emp;
import com.htf.util.DBUtils;
/**
* Paging query
* @author : htf
*
*/
public class TestPage {
public static final String FIND_BY_PAGE="select * from t_emp limit ?,?";
/**
* Paging query method
* @param pageNumber Page
* @param pageSize How many pieces of data to check on a page
* @return
*/
public List<Emp> findByPage(Integer pageNumber,Integer pageSize){
List<Emp> list=new ArrayList<>();
Connection conn=null;
PreparedStatement stmt =null;
ResultSet rs=null;
try {
conn=DBUtils.getConnection();
stmt=conn.prepareStatement(FIND_BY_PAGE);
// Page formula ->(pageNumber-1)*pageSize
stmt.setInt(1, (pageNumber-1)*pageSize);
stmt.setInt(2, pageSize);
rs=stmt.executeQuery();
// Query result set , Traverse to the set
while(rs.next()) {
Emp emp=new Emp();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setSal(rs.getDouble("sal"));
emp.setJob(rs.getString("job"));
emp.setDeptno(rs.getInt("deptno"));
list.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
// close resource
DBUtils.close(conn, stmt, rs);
}
return list;
}
public static void main(String[] args) {
List<Emp> list =new TestPage().findByPage(3, 5);
System.out.println(list.toString());
}
}
Entity class Emp
package com.htf.bean;
import java.util.Date;
/** The employee class
* @author : htf
*
*/
public class Emp {
private Integer empno;
private String ename;
private Double sal;
private String job;
private Integer deptno;
private Date hiredate;
public Emp(Integer empno, String ename, Double sal, String job,
Integer deptno, Date hiredate) {
super();
this.empno = empno;
this.ename = ename;
this.sal = sal;
this.job = job;
this.deptno = deptno;
this.hiredate = hiredate;
}
public Emp() {
super();
// TODO Auto-generated constructor stub
}
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 Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", sal=" + sal + ", job=" + job + ", deptno=" + deptno
+ ", hiredate=" + hiredate + "]";
}
}
边栏推荐
猜你喜欢
基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
Process architecture and process management
iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
Blue Bridge Cup: magic cube rotation [Vocational group]
Myrpc version 2
Interprocess communication
With the deep integration of cloud platform, the "Xueba" objectscale in the object storage industry is coming
史上最全的Redis基础+进阶项目实战总结笔记
File and IO
Share an example of a simple MapReduce method using a virtual machine
随机推荐
Equity interest [non DP]
Ora-00907: missing right parenthesis problem supplement
MySQL updates JSON string in array form
I spent three years in a big factory outsourcing, which subverted my understanding!
2021-07-14
File system and directory operations
Iterator of JS
Troubleshooting of abnormal communication between FortiGate and fortiguard cloud
oslo_ config. cfg. ConfigFileParseError: Failed to parse /etc/glance/glance-api. Conf: a solution to errors
Junior students summarize JS basic interview questions
What is the difference between synchronized and lock
01 backpack, dynamic planning
El upload upload file (manual upload, automatic upload, upload progress)
My idea configuration
小C的数组(array)
基于ROS的SLAM建图、自动导航、避障(冰达机器人)
网络层详解
Summary of the reasons why transactional doesn't work
idea灰屏问题
Threadlocal