MySQL implementation paging JSP + JavaBean implementation

xiaoxiao2021-03-06  41

Key code is a command under MySQL: SELECT * FROM table name limited (0 start), the number of records per page;

1. Establish 2 beans, one is responsible for database connections, one responsible for paging.

Package dbconn; import java.sql. *; import java.lang. *;

public class MySQLConnection {private String DBDriver = "com.mysql.jdbc.Driver"; private String ConnStr = "jdbc: mysql: // localhost:? 3306 / zb user = root & password = admin"; public MySQLConnection () throws java.lang .NullpointerException {Try {class.forname (dbdriver);} catch (classnotfoundexception e) {system.err.println (E.getMessage ());}}

Public string getConnStr () {return connStr;}}

Package dbconn; import java.sql. *; import java.lang. *; import dbconn.mysqlConnection. *;

Public class pagebreak = "", private recn = null; private project = null; private int pageize = 0; // 1 Page Several Record Private Int Pagenumber = 0; // Page Number

Public pagebreak () throws java.lang.nullpointerException {mysqlconnection dbconnection = new mysqlConnection (); connStr = dbconnection.getConnstr (); // Get connStr}

public ResultSet executeQuery (String sql) throws java.lang.NullPointerException {rs = null; try {conn = DriverManager.getConnection (ConnStr); Statement stmt = conn.createStatement (); rs = stmt.executeQuery (sql);} catch ( SQLEXCEPTION E) {system.err.println (E.GetMessage ());} Return RS;

public int RecordNumber () throws java.lang.NullPointerException // get the total number of records in the table {rs = null; int ColNumber = 0; try {conn = DriverManager.getConnection (ConnStr); Statement stmt = conn.createStatement (); rs = Stmt.executeQuery ("SELECT Count (*) as colnumber from user"); while (rs.next ()) {colnumber = rs.get ("colnumber");}}} catch (sqlexception e) {system.err.println (E.GetMessage ());} return color setpageSize // Set the page size, and calculate how many pages of pages {this.pageSize = ppagesize; int recordnumber = this.RecordNumber (); this .Pagenumber = (int) Math.floor (RecordNumber / Pagesize);

Public int getpagesize () // Get page size {Return this.pageSize;}

Public int getpagenumber () // Number of pages {Return this.pagenumber;

Public void close () // Displays close connection {Try {conn.close (); rs.close ();} catch (sqlexception e) {system.err.println (E.GetMessage ());}}}

2. Establish a JSP page

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ page import = "java.sql. *"%> <% dbconnection.setpageSize (2); // Settings page size

INT RecordperPage = dbconnection.getpagesize (); // Get page size int Pagenumber (); // Get page number int offset = 0; // SELECT, 0 Start Int next = 2; / / Connect the "Next page" initialization int pre = 1; / / Connect the initialization of "Previous Page" INT ABSOLUTEPAGE = 1; // Absolute page, 1 start. As a transition variable connected to "next page", "Previous"

IF (Request.GetParameter ("Pageno") == NULL) / / There is no parameter incoming, then the physical 1 page, 0Offset = 0; Else IF (Request.GetParameter ("Pageno")! = null) {OFFSET = INTEGER.PARSEINT (Request.getParameter ("Pageno")) - 1; // There is a parameter incoming, parameter is the logical page number, minus 1 physical page number IF ((Offset 1)> Pagenumber) // Direct One page {OFFSET = Pagenumber; absolute = absolutePage; pre = next-1;} else if (offset <1) // reach the first page {OFFSET = 0; absolutePage = Offset 1; Next = AbsolutePage 1; Pre = Next-1;} else {AbsolutePage = offSet 1; Next = AbsolutePage 1; Pre = AbsolutePage-1;}} String querySQL = "select * from user limit" offSet * RecordPerPage "," RecordPerPage; // Key code

ResultSet RS = dbconnection.executeQuery (querysql); while (rs.next ()) {string username = rs.getstring ("username"); out.print (username "
);}

OUT.PRINT (" Next
"); // Next out.print (" previous "); // Previous

Rs.close (); dbconnection.close ();%>

转载请注明原文地址:https://www.9cbs.com/read-68390.html

New Post(0)