您的位置:首页 > 数据库 > MySQL

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

2015-06-16 19:20 996 查看
转载请注明: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没错,和预期一样。然后输出结果也一致。

本节结束。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: