码迷,mamicode.com
首页 > 数据库 > 详细

java操作数据库:分页查询

时间:2017-05-06 13:14:06      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:color   add   auth   new   对象   分页查询   array   编号   数据库操作   

直接上。。。。

还是用之前的goods表,增加了一些数据

技术分享

1、实体类Goods

// 封装数据
public class Goods {
    private int gid;
    private String gname;
    private String gprice;
    private String gdate;
    public int getGid() {
        return gid;
    }
    public void setGid(int gid) {
        this.gid = gid;
    }
    public String getGname() {
        return gname;
    }
    public void setGname(String gname) {
        this.gname = gname;
    }
    public String getGprice() {
        return gprice;
    }
    public void setGprice(String gprice) {
        this.gprice = gprice;
    }
    public String getGdate() {
        return gdate;
    }
    public void setGdate(String gdate) {
        this.gdate = gdate;
    }
    public Goods(int gid, String gname, String gprice, String gdate) {
        super();
        this.gid = gid;
        this.gname = gname;
        this.gprice = gprice;
        this.gdate = gdate;
    }
    public Goods() {
        super();
        
    }
}

2、DBHelper类

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 获取数据库操作的连接对象
 * 关闭数据库操作的各种资源
 * @author 晏先政
 *
 */
public class DBHelper {
    private static final String className = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true";
    private static final String uname = "root";
    private static final String upass = "";
    
    /**
     * 获取数据库连接对象的方法
     */
    public static Connection getConn(){
        Connection conn = null;
        try{
            Class.forName(className);
            conn = DriverManager.getConnection(url,uname, upass);
        } catch(Exception e){
            e.printStackTrace();
        }
        
        return conn;
    }
    
    /**
     * 关闭数据库连接对象
     */
    public static void closeConn(Connection conn){
        try{
            if(conn!=null){
                conn.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * 关闭数据库操作对象
     */
    public static void closeStmt(Statement stmt){
        try{
            if(stmt!=null){
                stmt.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 关闭数据库操作对象
     */
    public static void closePstmt(PreparedStatement pstmt){
        try{
            if(pstmt!=null){
                pstmt.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    /**
     * 关闭数据库操作对象
     */
    public static void closeRs(ResultSet rs){
        try{
            if(rs!=null){
                rs.close();
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

3、实现类GoodsDao:操作数据库进行查询

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class GoodsDao {
    private Connection conn = null;
    private PreparedStatement pstmt = null;
    private ResultSet rs = null;
    // 查询当前页的数据
    public List<Goods> getListByCurPage(int curPage){
        List<Goods> list = new ArrayList<Goods>();
        try{
            conn = DBHelper.getConn();
            int num = (curPage-1)*5;
            String sql = "select * from goods limit "+num+",5";
            
            pstmt = conn.prepareStatement(sql);
            
            rs = pstmt.executeQuery();
            
            while(rs.next()){
                Goods ba = new Goods(rs.getInt("gid"),rs.getString("gname"),rs.getString("gprice"),rs.getString("gdate"));
                ba.setGid(rs.getInt("gid"));
                list.add(ba);
            }
            
        } catch(Exception e){
            e.printStackTrace();
        } finally{
            DBHelper.closeRs(rs);
            DBHelper.closePstmt(pstmt);
            DBHelper.closeConn(conn);
        }
        
        
        return list;
    }
    
    //  查询所有记录的总条数
    public int getCount(){
        int i = 0;
        try{
            conn = DBHelper.getConn();
            String sql = "select count(*) cnt from goods";
            
            pstmt = conn.prepareStatement(sql);
            
            rs = pstmt.executeQuery();
            
            if(rs.next()){
                i = rs.getInt("cnt");
            }
            
        } catch(Exception e){
            e.printStackTrace();
        } finally{
            DBHelper.closeRs(rs);
            DBHelper.closePstmt(pstmt);
            DBHelper.closeConn(conn);
        }
        return i;
    }
}

4、展示类GoodsShow

import java.util.List;
import java.util.Scanner;

public class GoodsShow {
    
    public static void main(String[] args) {
        GoodsShow bs = new GoodsShow();
        bs.show();
    }
    
    private Scanner input = new Scanner(System.in);
    private GoodsDao dao = new GoodsDao();

    int curPage = 1;
    List<Goods> list = null;
    public void show(){
        int rowCount = dao.getCount();
        
        int pageCount = rowCount%5==0?rowCount/5:rowCount/5+1;
        list = dao.getListByCurPage(curPage);
        
        print(list);
        
        System.out.println("首页【F】上一页【P】下一页【N】尾页【L】请选择:");
        char choose = input.next().toUpperCase().charAt(0);
        
        switch(choose){
            case ‘F‘:
                curPage = 1;
                break;
            case ‘P‘:
                curPage = curPage -1;
                if(curPage<1){
                    curPage = 1;                    
                }
                break;
            case ‘N‘:
                curPage = curPage +1;
                if(curPage>pageCount){
                    curPage = pageCount;
                }
                break;
            case ‘L‘:
                curPage = pageCount;
                break;
        }
        
        show();
    }
    
    public void print(List<Goods> list){
        System.out.println("编号\t商品\t价格\t时间");
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i).getGid()+"\t"+list.get(i).getGname()+"\t"+list.get(i).getGprice()+"\t"+list.get(i).getGdate());
        }
    }
}

then。。。。

技术分享

 

java操作数据库:分页查询

标签:color   add   auth   new   对象   分页查询   array   编号   数据库操作   

原文地址:http://www.cnblogs.com/Vito-Yan/p/6816317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!