Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use javabean to realize the pagination display of MySQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use javabean to achieve MySQL paging display", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use javabean to achieve MySQL paging display" bar!

Today, I wrote a javabean for MySQL pagination, which is realized by LIMIT in MySQL.

Sql = "SELECT * FROM Test LIMIT 5,10"

This sentence means to start with the fifth record and read down 10 records. This bean does not have the function of connecting to the database.

You can use your own class to link to the database, and of course you can use my poorly written dbClass.java to connect, ^ _ ^

The source code of the three programs is given here.

DbClass.java-used to connect to the MySQL database.

PageQuery.java-rewrites the ResultSet returned by dbClass to make it paging.

Example.jsp-- jsp file, you can see that I only used two lines to achieve the paging function, of course

Sql statements are not encouraged to be written directly in jsp, which is done here in order to make it clear to everyone.

Knowing that the level of self-knowledge is not high, I just want to throw a brick to attract jade, and I hope the master will point out any mistakes and omissions.

= = example.jsp = =

"+ bar+"

= = PageQuery.java = =

Package dbclass

/ * *

* PageQuery v 1.0

* this class was originally called TViewPage, written by sharetop, in php.

* colleague Macro has rewritten this class with PHP and added a lot of features.

* I feel the package is very good and easy to use. After using JSP, I have it.

* the idea is to rewrite it with JSP. This time, I have saved a lot of functions for the sake of brevity.

* try to make it easy to read and add more functions when you are free

*

* Mender:

* Jeru Liu

* Homepage:

* http://www.cyberlabs.com/~jeru/

* Email: jeru@163.net

*

* this class does not provide the ability to connect to the database, so you need to open the corresponding database externally.

* need to customize the data display format externally.

, /

Import java.util.*

Import java.sql.*

Import java.io.*

Import javax.servlet.*

Import javax.servlet.http.*

Public class PageQuery {

Int Offset; / / record offset

Total number of int Total; / / record

Int MaxLine; / / Records display the number of records per page

Results of ResultSet rs; / / readout

Int TPages; / / Total number of pages

Int CPages; / / current number of pages

String PageQuery; / / Paging displays the parameters to be passed

String Query; / / query statement

Query after String QueryPart; / / "FROM"

String FilePath

DbClass db; / / object of dbclass

/ / constructer do nothing

Public PageQuery () {

/ / display ten lines per page

MaxLine = 10

Db = new dbClass ()

}

/ / * read record *

/ / the main working function to read the corresponding records from the table according to the given conditions

Public ResultSet myQuery (String query, HttpServletRequest req) throws SQLException {

String query_part, os

Int begin, offset

/ / intercept query statements after "FROM"

Begin = query.indexOf ("FROM")

Query_part = query.substring (begin, query.length (). Trim ()

/ / calculate the offset

Os = req.getParameter ("offset")

If (os = = null) Offset = 0

Else Offset = Integer.parseInt (os)

/ / get the file name

FilePath = req.getRequestURI ()

Query = query

QueryPart = query_part

/ / calculate the total number of records

String SQL = "SELECT Count (*) AS total" + this.QueryPart

Rs = db.executeQuery (SQL)

If (rs.next ())

Total = rs.getInt (1)

/ / set the current and total number of pages

TPages = int) Math.ceil ((double) this.Total/this.MaxLine)

CPages = int) Math.floor ((double) Offset/this.MaxLine+1)

/ / judge according to the conditions, take out the required records

If (Total > 0) {

SQL = Query + "LIMIT" + Offset + "," + MaxLine

Rs = db.executeQuery (SQL)

}

Return rs

}

/ / display the total number of pages

Public int getTotalPages () {

Return TPages

}

/ / display the number of pages currently on

Public int getCurrenPages () {

Return CPages

}

/ / * display the page flip prompt bar *

/ / display the home page, the next page, the previous page and the last page

/ / you can change it to the style you like

Public String PageLegend () {

String str = ""

Int first, next, prev, last

First = 0

Next = Offset + MaxLine

Prev = Offset-MaxLine

Last = (this.TPages-1) * MaxLine

If (Offset > = MaxLine)

Str + = "Home Page"

Else str + = "Home Page"

If (prev > = 0)

Str + = "front page"

Else str + = "front page"

If (next < Total)

Str + = "back page"

Else str + = "back page"

If (TPages! = 0 & & CPages < TPages)

Str + = "last page"

Else str + = "last page"

Str + = "Page number:" + getCurrenPages () + "/" + getTotalPages () + "Page"

Str + = MaxLine + "posts / pages" + "Total" + Total + "articles"

Return str

}

}

= = dbClass.java = =

/ * *

* a class use to connect the MySQL database and do some query

* use mm.MySQL.Drive

* Jeru Liu, November 2, 2000, ver-1.1

*

, /

Package dbclass

Import java.sql.*

Public class dbClass {

/ / public: connection parameters

String dbName = "Kernel"

String Login = "root"

String Password = "MySQL"

String DBDriver = "org.gjt.mm.MySQL.Driver"

String ConnStr = "jdbc:MySQL://localhost/" + dbName+ "? user=" + Login+ "; password=" + Password

Connection con = null

Statement stmt = null

ResultSet rs = null

ResultSetMetaData resultsMeta = null

Int rows = 0

/ / public: constructor to load driver and connect db

Public dbClass () {

/ / load mm.MySQL.driver

Try

{

Class.forName ("org.gjt.mm.MySQL.Driver")

}

/ / display corresponding error message when onload error occur

Catch (java.lang.ClassNotFoundException e)

{

System.out.println ("Class not found exception occur. Message is:")

System.out.println (e.getMessage ())

}

/ / establish connection to the database throught driver

Try

{

Con = DriverManager.getConnection (ConnStr)

}

/ / display sql error message

Catch (SQLException e)

{

System.out.print ("SQL Exception occur. Message is:")

System.out.print (e.getMessage ())

}

}

/ / perform a query with records returned

Public ResultSet executeQuery (String sql) throws SQLException

{

ResultSet rs = null

Try

{

Stmt = con.createStatement ()

Rs = stmt.executeQuery (sql)

While (rs.next ())

This.rows + +

Rs = stmt.executeQuery (sql)

}

Catch (SQLException e)

{

System.out.print ("Query:" + e.getMessage ())

}

This.rs = rs

Return rs

}

/ / perform a query without records returned

Public boolean executeUpdate (String sql)

{

Try

{

Stmt = con.createStatement ()

Stmt.executeUpdate (sql)

Return true

}

Catch (SQLException e)

{

System.out.print ("Update:" + e.getMessage ())

Return false

}

}

/ / return the num of columns

Public int getColumns ()

{

Int columns = 0

Try

{

This.resultsMeta = this.rs.getMetaData ()

Columns = this.resultsMeta.getColumnCount ()

}

Catch (SQLException e) {}

Return columns

}

/ / return the num of rows

Public int getRows ()

{

Return this.rows

}

Public String getDBName () {

Return this.dbName

}

}

Undefinedundefined

Thank you for your reading, the above is "how to use javabean to achieve MySQL paging display" content, after the study of this article, I believe you on how to use javabean to achieve MySQL paging display of this problem has a deeper understanding, the specific use of the need for practice to verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report