JSP + Beans Pieces Display Record Battle

xiaoxiao2021-03-05  46

We use a beans to implement the paging display of the database record, and there are many ways. This example is a cursor of the ResultSet class to achieve paging.

This example is: page JSP, page display record

Page Beans

Database connection beans

This example is tested under Tomcat.

Key ideology:

Suppose the total number of records is m, the number of records shown per page is N, then the calculation formula of the total number of pages is:

Total page = (m% n) == 0? (M / n): (m / n 1);

If you are ready to display the contents of the P page, the cursor should be moved to (P-1) * n 1 record.

The source code is as follows:

1, page beans

Pagenumber.java

Package bean;

Public Class Pagenumber

{

Int rowcount = 1, // total record number

PageSize = 1, // Number of records displayed per page

ShowPage = 1, // Set the number of page numbers pre-displayed

PageCount = 1; // Total number after page

Public void setRowcount (int N) {

Rowcount = N;

}

Public int getRowcount () {

Return rowcount;

}

Public void setPageCount (int R, int P) {

RowCount = R;

Pagesize = P;

PageCount = (RowCount% PageSize) == 0? (RowCount / Pagesize) :( RowCount / Pagesize 1);

/ / Calculate the number of pages after paging

}

Public int getPagecount () {

Return PageCount;

}

Public void setshowpage (int N) {

Showpage = n;

}

Public int getShowpage () {

Return Showpage;

}

Public void setpagesize (int N) {

Pagesize = n;

}

Public int getpagesize () {

Return PageSize;

}

}

2, database connection beans

Package bean;

Import java.sql. *;

Import java.io. *;

Public class myconnection {

Connection con = NULL; // Declare a shared connection object

Statement stat = NULL;

ResultSet RS = NULL;

INT rowcount = 0; // Total number of records

Public myconnection () {

Try {

Class.Forname ("com.microsoft.jdbc.sqlser.sqlserverdriver);

} catch (classnotfoundexception e) {

System.out.print (e);

}

Try {

Con = DriverManager.getConnection ("JDBC: Microsoft: SQLServer: // localhost: 1433;

DatabaseName = student; "" SA "," ");

} catch (sqlexception e) {

System.out.print (e);

}

}

Public ResultSet MyQuery (String SQL) THROWS SQLEXCEPTION {

/ / The cursor of the result set can be moved up and down, but when the database changes, the result set is constant, // cannot use the result set to update the table in the database

Stat = con.createstatement (ResultSet.Type_Scroll_Sensitive,

ResultSet.concur_read_only);

/ / Return the rolling result set

RS = stat.executequery (SQL);

Return RS;

}

Public void myclose () throws sqlexception {

C. close ();

}

}

3, page display recorded JSP page

<% @ Page ContentType = "text / html; charset = GB2312"%>

<% @ Page Import = "java.sql. *"%>

<% @ Page Import = "bean.pagenumber"%>

<% @ Page Import = "bean.myconnection"%>

<% @ Page Import = "java.io. *"%>



<%!

// Display the method of database record in form

Public void showlist (ResultSet RS, JSPWriter Out, INT N) {

Try {

Out.print ("

");

String Number = rs.getstring (1);

Out.print ("

Learn Name Mathematics English Physical ");

For (INT i = 1; i <= n; i ) {

OUT.PRINT ("

" Number "");

String name = rs.getstring (2);

Out.print ("

" Name "");

INT MATH = rs.getint (3);

Out.print ("

" MATH "");

Int english = rs.getint (4);

Out.print ("

" ENGLISH "");

INT Physics = rs.getint (5);

Out.print ("

" Physics ""); OUT.PRINT ("");

rs.next ();

}

Out.print ("");

} catch (exception e) {

System.out.println (e);

}

}

%>

<%

ResultSet RS = NULL;

INT rowcount = 0; // Total number of records

Try {

/ / Return the rolling result set

RS = Mycon.MyQuery ("Select * from students");

// Move the cursor to the last line

Rs.last ();

// Get the total number of records

Rowcount = rs.getrow ();

/ / Set the number of records displayed per page

Handlepage.SetPageSize (2);

/ / Calculate the total number of pages

Handlepage.SetPageCount (Rowcount);

Out.print ("Again" HandlePage.getPageCount () "page,");

Out.print ("Display" Handlepage.getPageSize () "Sum Record");

} catch (sqlexception e) {

OUT.PRINT (E);

}

%>

<% - Select the form of a page -%>

<% String str = response.EncoderedirectURL ("showlist.jsp"); // Get the URL% of showlist.jsp>

Display Home:

Display Next:

Display Previous:

Enter the number of pages you want to display:

<%

String S = Request.getParameter ("a");

IF (s == NULL) {

s = "1";

} else if (S. Equals ("first")) {

Handlepage.SetShowPage (1); // Set the pre-display page

Out.print ("Currently Displayed Handlepage.getshowPage () " Page ");

// move the cursor to the specified location

Rs.absolute (1);

/ / Display the content of this page

Showlist (RS, OUT, HANDLEPAGE.GETPAGESIZE ());

} else if (S.Equals ("next")) {

INT n = handlepage.getshowpage (); // Get the current number of pages

n = (n 1); // increase the number of pages 1

IF (n> handlepage.getPageCount ()) n = 1;

HandlePage.SetShowPage (n); // Setup page

Out.print ("Currently Displayed Handlepage.getshowPage () " Page ");

// move the cursor to the specified location

rs.absolute ((n-1) * handlepage.getpagesize () 1);

/ / Display the content of the page

Showlist (RS, OUT, HANDLEPAGE.GETPAGESIZE ());

} else if (S.Equals ("previous")) {

INT n = handlepage.getshowpage (); // Get the current number of pages

n = n-1; // minus the number of pages 1

IF (n <= 0) n = handlepage.getPageCount ();

HandlePage.SetShowPage (n); // Setup page

Out.print ("Currently Displayed Handlepage.getshowPage () " Page ");

// move the cursor to the specified location

rs.absolute ((n-1) * handlepage.getpagesize () 1);

/ / Display the content of the page

Showlist (RS, OUT, HANDLEPAGE.GETPAGESIZE ());

} else {

INT m = integer.parseint (s); / / transform the input characters to integer

HandlePage.SetShowPage (M); // Setup page

Out.print ("Currently Displayed Handlepage.getshowPage () " Page ");

INT n = handlepage.getshowpage ();

// move the cursor to the specified location

rs.absolute ((n-1) * handlepage.getpagesize () 1);

/ / Display the content of the page

Showlist (RS, OUT, HANDLEPAGE.GETPAGESIZE ());

}

%>

The table structure of the database is:

Create Table Students

ID VARCHAR (10), Name Varchar (10), Math Int, ENGLISH INT, PHYSICS INT)

The database name is: student

Note: This example is connected to the SQL database, which is directly connected, requiring three SQL straight packages.

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

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