Java Operations Database: Paging query

Source: Internet
Author: User
Tags rowcount

Go directly to ....

Or use the previous goods table to add some data

1. Entity class goods

//Encapsulating Data Public classGoods {Private intGID; PrivateString Gname; PrivateString Gprice; PrivateString gdate;  Public intGetgid () {returnGID; }     Public voidSetGid (intGID) {         This. GID =GID; }     PublicString Getgname () {returnGname; }     Public voidsetgname (String gname) { This. Gname =Gname; }     PublicString Getgprice () {returnGprice; }     Public voidSetgprice (String gprice) { This. Gprice =Gprice; }     PublicString getgdate () {returngdate; }     Public voidsetgdate (String gdate) { This. gdate =gdate; }     PublicGoods (intgid, String gname, String gprice, String gdate) {        Super();  This. GID =GID;  This. Gname =Gname;  This. Gprice =Gprice;  This. gdate =gdate; }     PublicGoods () {Super(); }}

2, DBHelper class

Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.Statement;/*** Get connection objects for database operations * Close various resources for database operations *@authorYanchen **/ Public classDBHelper {Private Static FinalString className = "Com.mysql.jdbc.Driver"; Private Static FinalString url = "Jdbc:mysql://localhost:3306/test?characterencoding=utf8&usessl=true"; Private Static FinalString uname = "root"; Private Static FinalString UPass = ""; /*** Ways to get database connection objects*/     Public StaticConnection Getconn () {Connection conn=NULL; Try{class.forname (className); Conn=drivermanager.getconnection (Url,uname, UPass); } Catch(Exception e) {e.printstacktrace (); }                returnConn; }        /*** Close Database Connection object*/     Public Static voidCloseconn (Connection conn) {Try{            if(conn!=NULL) {conn.close (); }        } Catch(Exception e) {e.printstacktrace (); }    }        /*** Close Database Operations Object*/     Public Static voidclosestmt (Statement stmt) {Try{            if(stmt!=NULL) {stmt.close (); }        } Catch(Exception e) {e.printstacktrace (); }    }    /*** Close Database Operations Object*/     Public Static voidclosepstmt (PreparedStatement pstmt) {Try{            if(pstmt!=NULL) {pstmt.close (); }        } Catch(Exception e) {e.printstacktrace (); }    }        /*** Close Database Operations Object*/     Public Static voidClosers (ResultSet rs) {Try{            if(rs!=NULL) {rs.close (); }        } Catch(Exception e) {e.printstacktrace (); }    }}

3, implementation of class Goodsdao: operation of the database to query

Importjava.sql.Connection;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.util.ArrayList;Importjava.util.List; Public classGoodsdao {PrivateConnection conn =NULL; PrivatePreparedStatement pstmt =NULL; PrivateResultSet rs =NULL; //querying data for the current page     PublicList<goods> Getlistbycurpage (intcurpage) {List<Goods> list =NewArraylist<goods>(); Try{conn=Dbhelper.getconn (); intnum = (curPage-1); String SQL= "SELECT * FROM goods limit" +num+ ", 5"; Pstmt=conn.preparestatement (SQL); RS=Pstmt.executequery ();  while(Rs.next ()) {Goods ba=NewGoods (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); }                        returnlist; }        //query total number of records     Public intGetCount () {inti = 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); }        returni; }}

4, Display class Goodsshow

Importjava.util.List;ImportJava.util.Scanner; Public classGoodsshow { Public Static voidMain (string[] args) {goodsshow bs=Newgoodsshow ();    Bs.show (); }        PrivateScanner input =NewScanner (system.in); PrivateGoodsdao DAO =NewGoodsdao (); intCurpage = 1; List<Goods> list =NULL;  Public voidShow () {intRowCount =Dao.getcount (); intPageCount = rowcount%5==0?rowcount/5:rowcount/5+1; List=dao.getlistbycurpage (curpage);                print (list); System.out.println ("Home" F "previous" P "Next" N "last" L "Please select:"); CharChoose = Input.next (). toUpperCase (). CharAt (0); Switch(choose) { CaseF: Curpage= 1;  Break;  CaseP: Curpage= CurPage-1; if(curpage<1) {Curpage= 1; }                 Break;  CaseN: Curpage= Curpage +1; if(curpage>PageCount) {Curpage=PageCount; }                 Break;  CaseL: Curpage=PageCount;  Break;    } show (); }         Public voidPrint (list<goods>list) {System.out.println ("No. \ t goods \ t price \ t Time");  for(intI=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 Operations Database: Paging query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.