Reading notes for "JSP page display database"

xiaoxiao2021-03-06  86

This article describes the paging display of data with JavaBean and JSP pages, and the database used in the example is mysql.

1, look at Javabean first

Class name:

DatabaseBean.java:

The following is the code of DatabaseBean.java:

Package Database_basic; import java.sql. *; import java.util. *;

public class databaseBean {// This is the default database connection 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 () {}

// can be set flexibly connected public void setDBLocation database (String location) {DBLocation = location;} set by the method 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) {} * /

// Establish a connection public string dbconnect () {string strexc = "Success!"; // strexc defaults to success, if there is exception thrown, that is, the database connection is unsuccessful, then the following caution is given to other thrown information TRY {Class.Forname (dbdriver); conn = drivermanager.getConnection (dblocation);} catch (classnotfoundexception e) {strexc = "database driver is not found, error tips:
" E.TOSTRING ();} catch (SQLException e) {

strexc = "SQL statement error, error prompt
" E.TOSTRING ();} catch (exception e) {strexc = "error message:
" E.toString ();

Return (streen end of dbconnect)} //

// Disconnect PUBLIC STRING DBDISCONNECT () {string strexc = "Success!"; // strexc defaults to success, if there is exception thrown, that is, the database is unsuccessful, then the following caution is given to other throws Information Try {if (conn! = Null) conn.close ();} catch (sqlexception e) {strexc = e.tostring ();} return (strexc);

}

// Returns a result set PUBLIC RESULTSET Query (String SQL), Exception {ResultSet RS = NULL; if (conn == null) {DBCONNECT ();

IF (conn == null) {r = null;} else {Try {statement ();} catch (sqlexception e) {throw new SQLEXCEPTION ("COUND NOTEXECUTE Query. ");} catch (exception e) {throw new Exception (" COUND NOT EXECUTE Query. ");}} // Then End of iFreturn (RS);} // THEN End of The Function ExecuteQuery

// By incurring a SQL statement and Pagesize (number of results per page) and returns a total page number PUBLIC INT GETTOTALPAGE (STRING SQL, INT PAGESIZE) {ResultSet RS = NULL; INT TOTALROWS = 0; IF (conn = = NULL) {dbconnect ();

IF (conn == null) {r = null;} elsetry {statement s = conn.createstatement ();

RS = S.ExecuteQuery (SQL); // Get results set by incoming SQL

While (rs.next ()) TotalRows ; // Let RS one number, the number is over again, and the total entry of the total entry of the return result is calculated by TotalRows } catch (sqlexception e) {}

Rs = null; // The total number of pages (TOTALROWS-1) / PageSize 1 is obtained by this algorithm, and the result is returned. TotalRows refers to the total number of entries in the return result set, which refers to the number of entries ((TotalRows-1) / Pagesize 1).

// Get a result set PUBLIC RESUTSET GETPAGEDRS (string sql, int pageSize, int pagenumber) {resultEt = null; int absolutelocation; IF (conn = = NULL) {dbconnect ();

IF (conn == null) {r = null;} elsetry {statement s = conn.createstatement ();

// Pagesize * Pagenumber The number of entries displayed per page is multiplying by page, calculating the number of the last line of results, and any number greater than this maxROWS will be by Drops.SetMaxRows (PageSize * Pagenumber); RS = S.ExecuteQuery (SQL); } catch (sqlexception e) {}

// AbsoluteLuteLocation = pageSize * (PAGENUMBER-1) This expression calculates the number of the last result of the previous page (if there is this page, the result of the result entry "of the displayed items of the previous page is PageSoluteLuteLocation = pageSize * (Pagenumber -1);

Try {

/ / This FOR loop is to allow the result set RS positioning the last result before this page for FOR (INT i = 0; I

}

Public String Execute_sql (String SQL) {string strexc; strexc = "success!";

IF (conn! = null) {Try {preparedStatement update; Update = conn.preparestatement (SQL); Update.execute ();} catch (sqlexception e) {strexc = e.tostring ();} catch (Exception E) { Strexc = e.toString ();}} else {strexc = "connection lost!";} return (strexc);

} // Execute_SQL

2, analyze 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 below the Database_basic package, the alias is BasicDB <% string SQL ResultSet RS; INTSET RS; INT; String Reply, Exc; EXC = BasicDb.dbconnect (); // Established connection, if successful, return to Success! If it fails, return the corresponding error message

IF (! "" "") {// BasicDb.dbdisconnect (); throw new exception (exc);

INT PageSize = 10; // Define the number of data clauses per page int currentPage = 1; // The current page (the first display is definitely the first page! ~), the "current page" appears below Pages Parameters in the page are incorporated into int allPage = -1; string pages = request.getParameter ("Pages"); // Number of pages parameters in the page, the page representative of this parameter is to display "Current Page" IF (PAGES) ! = NULL) CURRENTPAGE = INTEGER.VALUEOF (Pages) .intValue (); // This is an example of an Integer-type INT type. When this page is executed, the parameter is null, currentpage = 1; When this page is executed again, the parameter pages will be assigned, currentpage = pages INT value

SQL = "Select * from gbook Order by id"; // This returned result ranted in DESC descending order, the advantage is that the latest information is displayed in front

Allpage = BasicDb.gettotalpage (SQL, Pagesize); // Get the total number of page codes

RS = BasicDb.getPageDRS (SQL, Pagesize, CURRENTPAGE); // Get the result set to display by the current page

%>

<% while (rs.next ()) {id = rs.getint ("id") ; // Get the ID number%>
name: <% = rs.getstring ("leaver")%> Time: <% = rs.getstring ("leave_date")%> email: <% = rs.getstring ("email")%> < Div style = "width: 150; overflow: hidden;"> Home: <% = rs.getstring ("homepage")%>
Content:

<% = rs.getString ( "content")%> <% }%>

now is the current = CURRENTPAGE%> Page, <% IF (CURRENTPAGE> 1) {%> " i "");

%> <% IF (CurrentPage Web Programming - JSP-> achieve JSP pages display database (mysql) author: Fred Huang *********************************** *********************************************************** *******************************************


New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.036, SQL: 9