java操作数据库:分页查询

直接上。。。。

还是用之前的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。。。。

 

转载于:https://www.cnblogs.com/Vito-Yan/p/6816317.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值