A common JSP paging class with the function of displaying multiple page numbers

Source: Internet
Author: User

I have learned jsp very well over the past few days. I have written a paging class to share it with you.

Part 1: db. MysqlConn database connection class

/*************************************** *******************

Path: zon. db. MysqlConn

File: MysqlConn. java

Purpose: Database Connection

Package db; import java. SQL. *; import javax. xml. parsers. *; import org. w3c. dom. *; import org. xml. sax. SAXException; import java. io. *; // public class MysqlConn {private Connection conn; // Connection object private Statement stmt; // Statement object private ResultSet rs; // result set object private String MySqldriver = "com. mysql. jdbc. driver "; // MYSQL Server Driver String private String MySqlURL =" jdbc: mysql: // "; // MYSQL Server connection String da Tabasex; String webRealPath = "D: // zon // WebRoot //"; // The actual path of the root path, which is used to read the database configuration file of the database Link parameter. xml // defines the Database Link parameter variable String server = "localhost"; String dbname = "phpzon"; String user = "admin"; String pass = "admin "; string port = "3306"; String dbchara = "UTF-8"; String dbprefix = "zon _"; // table prefix public void setDatabasex (String databasex) {this. databasex = databasex;} public String getDatabasex () {return databasex;} publ Ic void setWebRealPath (String webRealPath) {this. webRealPath = webRealPath;} public String getWebRealPath () {return webRealPath ;} /*************************************** * **************************** use com. mysql. jdbc. driver * This method gets the various parameters required for the connection and forms a connection string * And then establishes a connection * server; dbname, user, pass, port * represents the MYSQL server address, database name, user name, password, port, * this method has been modified, directly through the configuration file WEB-INF // database. to read link information in xml ************************** **************************************** **/Public Connection getMysqlConn () {// obtain the database Link parameter database based on the database configuration file. xml // create resolution factory DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); factory. setIgnoringElementContentWhitespace (true); // ignore spaces in the element content // create the parser DocumentBuilder dbxml; try {dbxml = factory. newDocumentBuilder (); // obtain the parsing file Document doc; try {doc = dbxml. parse ("file: //" + webRealPath +" WEB-INF // database. xml "); doc. normalize (); // get the root Element // Element root = doc. getDocumentElement (); // get all db elements // NodeList dbs = doc. getElementsByTagName ("db"); // NodeList dbType = doc. getElementsByTagName ("type"); NodeList dbDriver = doc. getElementsByTagName ("driver"); NodeList dbUrl = doc. getElementsByTagName ("url"); NodeList dbServer = doc. getElementsByTagName ("server"); NodeList dbPort = doc. getElement SByTagName ("port"); NodeList dbDbname = doc. getElementsByTagName ("dbname"); NodeList dbUser = doc. getElementsByTagName ("user"); NodeList dbPassword = doc. getElementsByTagName ("password"); NodeList dbChara = doc. getElementsByTagName ("chara"); NodeList dbPrefix = doc. getElementsByTagName ("prefix"); // obtain the parameter values int I = 0; MySqldriver = dbDriver. item (I ). getFirstChild (). getNodeValue (); // MYSQl driver MySqldriver = "Com. mysql. jdbc. driver "; MySqlURL = dbUrl. item (I ). getFirstChild (). getNodeValue (); // link path "jdbc: mysql: //"; server = dbServer. item (I ). getFirstChild (). getNodeValue (); // database server address port = dbPort. item (I ). getFirstChild (). getNodeValue (); // default database port 3306 dbname = dbDbname. item (I ). getFirstChild (). getNodeValue (); // Database Name user = dbUser. item (I ). getFirstChild (). getNodeValue (); // user name pass = dbPassword. item (I ). get FirstChild (). getNodeValue (); // password dbchara = dbChara. item (I ). getFirstChild (). getNodeValue (); // database code dbprefix = dbPrefix. item (I ). getFirstChild (). getNodeValue (); // table name prefix try {// complete connection string MySqlURL = MySqlURL + server + ":" + port + "/" + dbname + "? User = "+ user +" & password = "+ pass +" & useUnicode = true & characterEncoding = "+ dbchara; Class. forName (MySqldriver); conn = DriverManager. getConnection (MySqlURL);} catch (Exception e) {System. out. println ("database operation error, please check carefully"); System. err. println (e. getMessage () ;}} catch (SAXException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} catch (ParserConfigurationException e1) {// TODO Auto-generated catch block e1.printStackTrace ();} return conn;}/** execute the SQL statement and return the record set, mainly for select and show statements */public ResultSet sqlQuery (String SQL) {SQL = SetQuery (SQL); Statement temStmt = null; // Statement object ResultSet temRs = null; // result set object Connection temConn = this. getMysqlConn (); try {temStmt = temConn. createStatement (); // execute the SQL query statement to obtain the record set temrs1_temstmt.exe cuteQuery (SQL);} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} return temRs;}/** execute the Insert SQL statement */public void sqlInsert (String SQL) {SQL = SetQuery (SQL ); try {this.getmysqlconn().createstatement(cmd.exe cuteUpdate (SQL);} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}/ ** execute the update SQL statement */public void sqlUpdate (String SQL) {SQL = SetQuery (SQL); try {this.getmysqlconn().createstatement(cmd.exe cuteUpdate (SQL );} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}/ ** execute the delete SQL statement */public void sqlDelete (String SQL) {SQL = SetQuery (SQL); try {this.getmysqlconn().createstatement(cmd.exe cuteUpdate (SQL );} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace () ;}/ ** Execute SQL statements and return the total number of records, mainly for select statements */public int sqlTotalRow (String SQL) {Statement temStmt = null; // statement object ResultSet temRs = null; // result set object int result = 0; // result value Connection temConn = this. getMysqlConn (); try {temStmt = temConn. createStatement (); // execute the SQL query statement to obtain the record set temrs1_temstmt.exe cuteQuery (SQL); temRs. last (); result = temRs. getRow ();} catch (SQLException e) {// TODO Auto-generated catch block e. printStackTrace ();} return result;} // when you set an SQL statement, dede _ in the SQL statement is automatically replaced with the table prefix zon_public String SetQuery (String SQL) in the configuration file) {String prefix = "dede _"; SQL = SQL. replace (prefix, this. dbprefix); return SQL;} // close the database connection public void close () {try {// rs. close (); // stmt. close (); conn. close ();} catch (SQLException sqlexception) {sqlexception. printStackTrace ();}}}

Part 2: db. MysqlPages paging code

/*************************************** * ******************* Path: zon. db. mysqlPages file: MysqlPages. java purpose: optimized pagination ************************************* * *********************/package db; import java. SQL. *; import db. mysqlConn; // database link class /********************************** * ************************* function: * MARK: * Author: Liu yadong Yodon. liu * Date: 09:20:12 ************************************* * *********************/p Ublic class MysqlPages {int PageSize = 10; // variables can be customized during the call, and the number of entries per page int PageRows = 8; // variables can be customized during the call, page number group, the number of page numbers displayed in each group. The default value is 8. The actual number of displayed pages is automatically + 1, including the current page number int RowCount = 0; // The total number of entries int PageCount = 1; // The total number of pages int inPage = 1; // the current page number defaults to int inRow = 0; // the start line number of this page query defaults to 0 String linkPage = "? "; // You can customize variables when calling. The page number link page String webRealPath ="/"; // The actual root path of the website, after obtaining the information from the jsp page, pass it over // display text by default. You can customize the variable String langs = "homepage, Previous Page, next page, last page, total, record, and page" during the call "; string [] langss = langs. split (","); String lang_first_page = langss [0]; String lang_pre_page = langss [1]; String lang_next_page = langss [2]; String lang_end_page = langss [3]; string lang_total = langss [4]; String lang_record_number = langss [5]; String lang_page = lan Gss [6]; // temporary String SQL = ""; String page = "1"; private Statement stmt; // Statement object private ResultSet rs; // result set object // return value, page number list String pagesInfo = "";/***** main method to obtain the page list String * @ return */public String pageInfo () {MysqlConn DBConn = new MysqlConn (); // create a database link instance. You can modify RowCount = DBConn according to your actual situation. sqlTotalRow (SQL); // There Is A sqlTotalRow method in MysqlConn, used to calculate the total record (Please modify here according to your own way of calculating the total record) PageCount = (RowCount + PageSize-1) /PageSize; If (page! = Null) inPage = Integer. parseInt (page); if (inPage <1) inPage = 1; if (inPage> PageCount) inPage = PageCount; inRow = (inPage-1) * PageSize; SQL = SQL + "limit" + String. valueOf (inRow) + "," + String. valueOf (PageSize); rs = DBConn. sqlQuery (SQL); // int PageRowsL = PageRows/2; // generate the maximum number of pages called on the left of the current page number int PageRowsR = PageRows-PageRows/2; // generate the maximum number of pages called to the right of the current page number if (inPage <= PageRowsL) {PageRowsL = inPage-1; PageRows R = PageRows-inPage + 1;} if (inPage + PageRowsR> PageCount) {PageRowsL = PageRows-(PageCount-inPage); PageRowsR = PageCount-inPage;} int PageLs = 1; // int PageRs = PageCount; // PageLs = (inPage-PageRowsL)> = 1 )? InPage-PageRowsL: 1; // The actual first page number displayed on the left PageRs = (inPage + PageRowsR) <= PageCount )? InPage + PageRowsR: PageCount; // The first page number displayed on the right: pagesInfo = pagesInfo + ("<div class = dh_pages> <ul> <li class = total>" + lang_total + "" + RowCount + lang_record_number + ", "+ PageCount + lang_page +" </li> "); // homepage pagesInfo = pagesInfo + ("<li> <a href ="/"mce_href ="/"+ linkPage +" page = 1/">" + lang_first_page + "</a> </li> "); // Previous page pagesInfo = pagesInfo + ("<li> <a href ="/"mce_href ="/"+ linkPage +" page = "+ (inPage-1) + "/"> "+ lang_pre_p Age + "</a> </li>"); // if (PageLs! = InPage) {for (int iL = PageLs; iL <inPage; iL ++) {pagesInfo = pagesInfo + ("<li> <a href ="/"mce_href ="/"+ linkPage +" page = "+ iL +"/">" + iL + "</a> </li> ");}} // current page pagesInfo = pagesInfo + ("<li class = inpage> <a href ="/"mce_href ="/"+ linkPage +" page = "+ inPage + "/"> "+ inPage +" </a> </li> "); // if (PageRs! = InPage) {for (int iR = inPage + 1; iR <= PageRs; iR ++) {pagesInfo = pagesInfo + ("<li> <a href ="/"mce_href ="/"" + linkPage + "page =" + iR + "/"> "+ iR + "</a> </li> ");}} // next page pagesInfo = pagesInfo + ("<li> <a href ="/"mce_href ="/"+ linkPage +" page = "+ (inPage + 1) + "/"> "+ lang_next_page +" </a> </li> "); // pagesInfo = pagesInfo + ("<li> <a href ="/"mce_href ="/"+ linkPage +" page = "+ PageCount + "/"> "+ lang_end_page +" </a> </li> "); pagesInfo = pagesInfo + ("</ul> </div>"); return pagesInfo;} public void setPage (String page) {this. page = page;} public String getPage () {return page;} public void setSql (String SQL) {this. SQL = SQL;} public String getSql () {return SQL;} public void setLinkPage (String linkPage) {this. linkPage = linkPage;} public String getLinkPage () {return linkPage;} public void setPageSize (int PageSize) {this. pageSize = PageSize;} public int getPageSize () {return PageSize;} public void setPageRows (int PageRows) {this. pageRows = PageRows;} public int getPageRows () {return PageRows;} public void setLangs (String langs) {this. langs = langs;} public String getLangs () {return langs;} public void setLang_first_page (String lang_first_page) {this. lang_first_page = lang_first_page;} public String getLang_first_page () {return lang_first_page;} public void setLang_pre_page (String lang_pre_page) {this. lang_pre_page = lang_pre_page;} public String getLang_pre_page () {return lang_pre_page;} public void setLang_next_page (String lang_next_page) {this. lang_next_page = lang_next_page;} public String getLang_next_page () {return lang_next_page;} public void setLang_end_page (String lang_end_page) {this. lang_end_page = lang_end_page;} public String getLang_end_page () {return lang_end_page;} public void setLang_total (String lang_total) {this. lang_total = lang_total;} public String getLang_total () {return lang_total;} public void setLang_record_number (String lang_record_number) {this. lang_record_number = lang_record_number;} public String getLang_record_number () {return lang_record_number;} public void setLang_page (String lang_page) {this. lang_page = lang_page;} public String getLang_page () {return lang_page;}/* String lang_first_page = "Homepage"; String lang_pre_page = "Previous Page"; String lang_next_page = "next page "; string lang_end_page = "last page"; String lang_total = "Total"; String lang_record_number = "record"; String lang_page = "page"; */public void setWebRealPath (String webRealPath) {this. webRealPath = webRealPath;} public String getWebRealPath () {return webRealPath;} public void setRs (ResultSet rs) {this. rs = rs;} public ResultSet getRs () {return rs ;}}

Part 3: JSP page call

<% // Set the attribute value DBPages. setPageSize (10); // number of entries per page DBPages. setPageRows (4); // the number of pages in each group, preferably an even number. The current page number is automatically added. // The image is set for different buttons. You can also set text directly, for example: homepage, Previous Page, next page, and last page DBPages. setLang_first_page ("

Part 4: style sheets are used to control the Display Effect of page numbers.

/* General paging style */. dh_pages {}. dh_pages ul {float: left; padding: 12px 0px 12px 16px ;}. dh_pages ul li {float: left; font-family: Tahoma; line-height: 17px; margin-right: 6px ;}. dh_pages ul li a {float: left; padding: 2px 4px 2px; color: #666; border-bottom: 1px solid # EEE ;}. dh_pages ul li a: hover {color: #690; text-decoration: none; padding: 2px 4px 1px; border-bottom: 2px solid #690 ;}. dh_pages ul li. inpage ,. pagebox ul li. inpage a: hover {color: # F63; padding: 2px 4px 1px; border-bottom: 2px solid # F63; font-weight: bold ;} /* The total page number and total page number in the page, which are usually left or right-aligned */. dh_pages. total {float: right; line-height: 21px; color: #999 ;}. dh_pages. total strong {color: #666; font-weight: normal; margin: 0px 2px ;}

Part 5: database Link parameter configuration file database. xml

In this example, the User-Defined configuration file of a database link is used to read the database and put in the WebRoot/WEB-INF/directory. The content is as follows:

<?xml version="1.0" encoding="utf-8"?>   <dbconfig>   <db id="1"><type>mysql</type><driver>com.mysql.jdbc.Driver</driver>   <url>jdbc:mysql://</url>  <server>localhost</server><port>3306</port>   <dbname>zon</dbname>  <user>admin</user>   <password>admin</password><chara>utf-8</chara>  <prefix>zon_</prefix>  </db> </dbconfig>

Related Article

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.