Java Database Operations: Paging query, java Paging
Directly ....
We still use the previous goods table to add some data.
1. Object Goods
// Encapsulate the public class Goods {private int gid; private String gname; private String uplice; 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 getuplice () {return uplice;} public void setuplice (String uplice) {this.. gdate = gdate;} public Goods (int gid, String gname, String uplice, String gdate) {super (); this. gid = gid; this. gname = gname; this. uplice = uplice; this. gdate = gdate;} public Goods () {super ();}}
2. DBHelper class
Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. statement; /*** get the connection object of the database operation * close various resources of the database operation * @ 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 = ""; /*** method for obtaining database Connection objects */public static Connection getConn () {Connection conn = null; try {Class. forName (className); conn = DriverManager. getConnection (url, uname, upass);} catch (Exception e) {e. printStackTrace ();} return conn;}/*** closes the database connection object */public static void closeConn (Conne Ction conn) {try {if (conn! = Null) {conn. close () ;}} catch (Exception e) {e. printStackTrace () ;}}/*** shut down the database operation object */public static void closeStmt (Statement stmt) {try {if (stmt! = Null) {stmt. close () ;}} catch (Exception e) {e. printStackTrace () ;}}/*** shut down the database operation object */public static void closePstmt (PreparedStatement pstmt) {try {if (pstmt! = Null) {pstmt. close () ;}} catch (Exception e) {e. printStackTrace () ;}}/*** shut down the database operation object */public static void closeRs (ResultSet rs) {try {if (rs! = Null) {rs. close () ;}} catch (Exception e) {e. printStackTrace ();}}}
3. Implementation class GoodsDao: Operate the database for query
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; // query the public List of data on the current page <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.exe cuteQuery (); while (rs. next () {Goods ba = new Goods (rs. getInt ("gid"), rs. getString ("gname"), rs. getString ("uplice"), 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;} // query the total number of records public int getCount () {int I = 0; try {conn = DBHelper. getConn (); String SQL = "select count (*) cnt from goods"; pstmt = conn. prepareStatement (SQL); rs = pstmt.exe cuteQuery (); 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. Display GoodsShow
Import java. util. list; import java. util. extends; public class GoodsShow {public static void main (String [] args) {GoodsShow bs = new GoodsShow (); bs. show ();} private partition input = new partition (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 ("homepage [F] Previous Page [P] Next page [N] End page [L] Select:"); 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 ("No. \ t product \ t price \ t time"); for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I ). getGid () + "\ t" + list. get (I ). getGname () + "\ t" + list. get (I ). getuplice () + "\ t" + list. get (I ). getGdate ());}}}
Then ....