問題:
使用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沒錯,和預期一樣。然後輸出結果也一致。
本節結束。。。。