前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用JDBC连接MySQL数据库--典型案例分析(八)----实现员工数据的分页查询

使用JDBC连接MySQL数据库--典型案例分析(八)----实现员工数据的分页查询

作者头像
MickyInvQ
发布2020-09-27 15:07:22
1.1K0
发布2020-09-27 15:07:22
举报
文章被收录于专栏:InvQ的专栏InvQ的专栏

转载请注明: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表中数据的分页查询,代码如下所示:

代码语言:javascript
复制
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没错,和预期一样。然后输出结果也一致。

本节结束。。。。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-06-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档