This article describes how to use javabean and jsp pages to display data by page. In this example, the database used is Mysql, if you need it, you can see that this article introduces how to use javabean and jsp pages to display data by page. In this example, the database used is Mysql.
1. Check javabean first.
Class Name:
DatabaseBean. java:
The code for databaseBean. java is as follows:
Package database_basic; import java. SQL. *; import java. util. *; public class databaseBean {// This is the default database connection method private String DBLocation = "jdbc: mysql: // localhost/onestoptech? User = root & password = password & useUnicode = true & characterEncoding = GB2312 "; private String DBDriver =" org. gjt. mm. mysql. driver "; private Connection conn = null; public databaseBean () {}// you can use the set method to flexibly set the Connection public void setDBLocation (String location) {DBLocation = location ;} public void setDBDriver (String driver) {DBDriver = driver;} public void setconn (Connection conn) {this. conn = conn;} public String getDBLocation () {Return (DBLocation);} public String getDBDriver () {return (DBDriver);} public Connection getconn () {return (conn);}/* public String DBConnect () {} public String DBDisconnect () {} public ResultSet query (String SQL) {} public int getTotalPage (String SQL, int pageSize) {} public ResultSet getPagedRs (String SQL, int pageSize, int pageNumber) {} public String execute_ SQL (String SQL) {} * // create a connection public String DBCo Nnect () {String strExc = "Success! "; // The default value of strExc is Success. If an exception is thrown, that is, the database connection is unsuccessful, other throws will be assigned to the catch below. try {Class. forName (DBDriver); conn = DriverManager. getConnection (DBLocation);} catch (ClassNotFoundException e) {strExc = "the database driver is not found. error message:
"+ E. toString ();} catch (SQLException e) {strExc =" SQL statement error, error prompt
"+ E. toString ();} catch (Exception e) {strExc =" error prompt:
"+ E. toString ();} return (strExc);} // then end of DBConnect // disconnect public String DBDisconnect () {String strExc =" Success! "; // The default value of strExc is Success. if an exception is thrown, that is, the database disconnection fails, other throws are assigned in the following catch: try {if (conn! = Null) conn. close ();} catch (SQLException e) {strExc = e. toString () ;}return (strExc) ;}// return a result set public ResultSet query (String SQL) throws SQLException, Exception {ResultSet rs = null by inputting an SQL statement; if (conn = null) {DBConnect () ;}if (conn = null) {rs = null;} else {try {Statement s = conn. createStatement (); rs=s.exe cuteQuery (SQL);} catch (SQLException e) {throw new SQLException ("Cound not execute query. ");} Catch (Exception e) {throw new Exception (" Cound not execute query. ") ;}} // then end of if return (rs );} // then end of the function executeQuery // the total number of pages public int getTotalPage (String SQL, int pageSize) {ResultSet rs = null; int totalRows = 0; if (conn = null) {DBConnect () ;}if (conn = null) {rs = null ;} else try {Statement s = conn. createStatement (); rs=s.exe cuteQuer Y (SQL); // obtain the result set while (rs. next () totalRows ++; // Let the rs count one by one. After the count is completed, the total number of entries in the returned result set is calculated through totalRows ++.} catch (SQLException e) {} rs = null; // the total number of pages (totalRows-1)/pageSize + 1 from this algorithm and return the result. TotalRows refers to the total number of entries in the returned result set, and pageSize refers to the number of entries displayed per page return (totalRows-1)/pageSize + 1);} // pass in the SQL statement, the number of entries (pageSize) and page number displayed on each page. A public ResultSet getPagedRs (String SQL, int pageSize, int pageNumber) {ResultSet rs = null; int absoluteLocation is obtained; if (conn = null) {DBConnect () ;}if (conn = null) {rs = null;} else try {Statement s = conn. createStatement (); // pageSize * pageNumber the number of entries displayed on each page multiplied by the page number to calculate the number of the result of the last row. Any result with a number greater than this maxrows will be dropped s. setMaxRows (pageSize * pageNumber); rsw.s.exe cuteQuery (SQL);} catch (SQLException e) {}// absoluteLocation = pageSize * (pageNumber-1) this expression calculates the number of the last result of the previous page (if there is a previous page, the number of results displayed on the previous page must be pageSize) absoluteLocation = pageSize * (pageNumber-1 ); try {// This for loop is used to locate the result set rs to the last result place before this page for (int I = 0; I2. Analyze the jsp page
Page name:
Admin_show.jsp
Page code:
<% @ Page errorPage = "error. jsp "%> <% @ page contentType =" text/html; charset = gb2312 "%> <% @ page import =" java. util. * "%> <% @ page import =" java. SQL. * "%> // import the databaseBean class under the database_basic package. The alias is basicDB.
<% String SQL; ResultSet rs; int id; String reply, Exc; Exc = basicDB. DBConnect (); // create a connection. If the connection succeeds, Success is returned! If the request fails, the system returns the error message if (! Exc. equals ("Success! ") {// BasicDB. DBDisconnect (); throw new Exception (Exc);} int pageSize = 10; // defines the number of data entries displayed on each page. int currentPage = 1; // current page (the first page must be displayed! ~), The "Current page" will be passed in int allPage =-1 by the pages parameter in the following page; String pages = request. getParameter ("pages"); // obtain the pages parameter on the page. This parameter indicates the current page to be displayed if (pages! = Null) currentPage = Integer. valueOf (pages ). intValue (); // This is an example of converting Integer type to int type. When the page is executed for the first time, the parameter pages is null, currentPage = 1; when this page is executed again, the pages parameter is assigned a value. The int value of currentPage = pages is SQL = "select * from gbook order by id desc "; // In this way, the returned result is sorted in descending order by desc. The advantage is that the latest information is displayed, allPage = basicDB. getTotalPage (SQL, pageSize); // obtain the total number of page numbers rs = basicDB. getPagedRs (SQL, pageSize, currentPage); // obtain the result set to be displayed on the current page. %>
<% While (rs. next () {id = rs. getInt ("id"); // obtain the id in the database (result set). %>
| Name: <% = rs. getString ("leaver") %> |
Time: <% = rs. getString ("leave_date") %> |
Email: <% = rs. getString ("email") %> |
Home: <% = rs. getString ("homepage") %> |
Content:
<% = Rs. getString ("content") %> |
<% }%>
|
| Page <% = currentPage %>, <% if (currentPage> 1) {%> "> Homepage <%} for (int I = 1; I <= allPage; I ++) {// 1, 2, 3, 4 ...... Link out. println ("" + I + "") ;}%> <% if (currentPage "> Last page <% }%> |
} // Then end of the class
3. Summary
In this paging display program, there are several algorithms and implementation methods that are relatively fixed and classic. For those who have not written paging programs, they should be inspired.
For more information about how to use jsp to read data from the database, see the PHP Chinese website!