使用JDBC连接MySQL数据库--典型案例分析(八)----实现员工数据的分页查询
时间:2022-07-25
本文章向大家介绍使用JDBC连接MySQL数据库--典型案例分析(八)----实现员工数据的分页查询,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
转载请注明:http://blog.csdn.net/uniquewonderq
问题:
使用JDBC连接Mysql数据库,实现对Emp表数据的分页查询功能。
方案:
对于较大的数据量,通常采用分页查询的方式。不同的数据库产品有不同的数据库级的分页查询策略。例如:Oracle通常使用rownum的方式;而Mysql使用limit的方式。
Oracle采用rownum和子查询实现分页查询,SQL语句如下,
select * from (select rownum rn,empno,ename,job,mgr,hiredate,sal,comm,deptno from (select * fron emp order by empno))where rn between 6 and 10
上述SQL语句的功能为按照员工编号升序员工信息,获取排序后第6到10 位之间的5条员工信息。
实现上述功能的MySQL数据库的SQL语句如下:
select * from emp order by empno limit 5,5;
MYSQL中使用limit关键字实现分页查询。其中,limit后第一个参数为开始获取数据的行号(从0开始),第二个参数为获取记录的行数。第二个参数可省略,表示从第一个参数开始,获取后续所有记录。
步骤:
实现此案例需要按照如下步骤进行。
步骤:添加方法findByPageMySQL方法,实现连接Mysql数据库,实现对Emp表中数据的分页查询,代码如下所示:
package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import com.sun.org.apache.regexp.internal.recompile;
import Entity.Emp;
public class EmpDAO {
public static void main(String [] args){
EmpDAO dao=new EmpDAO();
//1.select all
//dao.findAll();
//2.insert
//Emp emp=new Emp(1001,"rose","Analyst",7901,"2014-05-01",3000.00,500.00,10);
//System.out.println("emp.getEmpNo()"+emp.getEmpNo());
//dao.add(emp);
//3.update
//emp.setSal(4500.00);
//dao.update(emp);
//4.findByPageMysql
dao.findByPageMySQL(2, 3);//查看第二页,每页3条
}
public void findByPageMySQL(int page,int pageSize){
Connection con=null;
PreparedStatement stmt=null;
ResultSet rs=null;
int total=-1;//总记录数
int pages=-1;//总页数
String sql_total="select count(*) from emp";
String sql="select * from emp order by empno limit ?,?";
try {
con=ConnectionSource.getConnection();
stmt=con.prepareStatement(sql_total);
//获得总的记录数
rs=stmt.executeQuery();
if(rs.next()){
total=rs.getInt(1);
}
System.out.println("总记录数为:"+total);
//计算总共多少页
int mod=total%pageSize;
if(mod==0){
pages=total/pageSize;
}
else pages=total/pageSize +1;
//如果要查看的页数大于最大页,或者小于1,则取最后一页或第一页
if(page>pages){
page=pages;
}else if(page<1){
page=1;
}
System.out.println("sql语句为:"+sql);
int start=(page-1)*pageSize;
stmt=con.prepareStatement(sql);
stmt.setInt(1, start);
stmt.setInt(2, pageSize);
rs=stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt("empno")+","+rs.getString("ename")+","+rs.getDouble("sal")+","+rs.getDate("hiredate"));
}
} catch (SQLException e) {
System.out.println("数据库访问异常!");
throw new RuntimeException(e);
}finally{
try {
if(stmt!=null){
stmt.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
System.out.println("释放资源时发生异常!");
}
}
}
public void findAll(){
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try {
con=ConnectionSource.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery("select empno,ename,sal,hiredate from emp;");
while(rs.next()){
System.out.println(rs.getInt("empno")+","+rs.getString("ename")+","+rs.getDouble("sal")+","+rs.getDate("hiredate"));
}
} catch (SQLException e) {
System.out.println("数据库访问异常!");
throw new RuntimeException(e);
}
finally{
try {
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
System.out.println("释放资源时发生异常!");
}
}
}
public void add(Emp emp){
Connection con=null;
Statement stmt=null;
int flag=-1;
String sql="insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values("+emp.getEmpNo()+","+"'"+emp.getEname()+"',"+"'"+emp.getJob()+"',"+emp.getMgr()+","+"str_to_date('"+emp.getHiredate()+"','%Y-%m-%d %H:%i:%s'),"+emp.getSal()+","+emp.getComm()+","+emp.getDeptno()+")";
try {
con=ConnectionSource.getConnection();
stmt=con.createStatement();
flag =stmt.executeUpdate(sql);
//Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing,
//such as an SQL DDL statement.
//either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0
//for SQL statements that return nothing
//这个flag返回有两种情况:1.返回执行完的行数
//如果是DDL语句那么什么都不返回。
//DDL语句:Data Definition Language
//比如:CREATE DATABASE,CREATE TABLE,ALTER TABLE ,DROP TABLE,CREATE VIEW,ALTER VIEW ,DROP VIEW 等
if(flag>0){
System.out.println("新增记录成功!");
}
} catch (SQLException e) {
System.out.println("数据库访问异常!");
throw new RuntimeException(e);
}
finally{
try {
if(stmt!=null){
stmt.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e2) {
System.out.println("释放资源发生异常!");
}
}
}
public void update(Emp emp){
Connection con=null;
Statement stmt=null;
int flag=-1;
String sql="update emp set sal="+emp.getSal()+","+"comm="+emp.getComm()+"where empno="+emp.getEmpNo();
try {
con=ConnectionSource.getConnection();
stmt=con.createStatement();
flag=stmt.executeUpdate(sql);
if(flag>0){
System.out.println("更新记录成功!");
}
} catch (SQLException e) {
System.out.println("数据库访问异常!");
throw new RuntimeException(e);
}finally{
try {
if(stmt!=null){
stmt.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e2) {
System.out.println("释放资源发生异常!");
}
}
}
}
执行上述代码:
由表可看出:第三条是7499
运行结果:
总记录数为11没错,和预期一样。然后输出结果也一致。
本节结束。。。。
- 详解ANGULAR2组件中的变化检测机制(对比ANGULAR1的脏检测)
- 如何快速查看github代码库中第一次commit的记录
- 更换包管理工具npm为yarn
- 【精选】使用Cryptory分析影响加密货币价格的因素(区块链系列3)
- React -- 组件间通信
- 图片和视频防盗链简单介绍
- 对比cp和scp命令 将数据从一台linux服务器复制到另一台linux服务器
- laravel—用Migration的操作数据库
- 有货移动Web端性能优化探索实践
- webpack打包速度和性能再次优化
- MySQL 清除表空间碎片
- 解决ios不支持按钮:active伪类的方法
- HTTP-FLV直播初探
- BZOJ1058: [ZJOI2007]报表统计
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- Android开发中的文件操作工具类FileUtil完整实例
- Android开发中超好用的正则表达式工具类RegexUtil完整实例
- Android ijkplayer的使用方法解析
- Android开发实现查询远程服务器的工具类QueryUtils完整实例
- 解决android studio 3.0 加载项目过慢问题–maven仓库选择
- Android实现朋友圈点赞列表
- Kotlin基本类型自动装箱一点问题剖析
- Kotlin入门教程之开发环境搭建
- Android:Field can be converted to a local varible.的解决办法
- Android使用多线程进行网络聊天室通信
- android实现banner轮播图无限轮播效果
- Android CheckBox中设置padding无效解决办法
- Android开发中使用颜色矩阵改变图片颜色,透明度及亮度的方法
- Android手势左右滑动效果
- Android开发实现根据包名判断App运行状态的方法