This function creates two JavaBean components and a JSP page display page. The first is to process the an connected to the database, and the first Javabean is the code used to process the results of the paging query, the third JSP is to call the second JavaBean and display the paging query results!
// The following is a JavaBean code connecting to the MySQL database (you can change the following database without affecting code execution ):
Package data;
Import java. SQL .*;
Public class logindata {
Connection conn = NULL;
Public logindata (){
This. Connect ();
}
Public connection getconn (){
Return this. Conn;
}
Public Boolean connect (){
Try {
// Use the JDBC bridge to create a database connection
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
// Use the getconnection () method of the drivermanager class to establish a connection
// The first parameter defines the user name and the second parameter defines the password
This. Conn = java. SQL. drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/logindemo? Useunicode = true & amp; characterencoding = gb2312 "," root "," 123456 ");
} Catch (exception ex ){
Ex. printstacktrace ();
Return false;
}
Return true;
}
}
// Pagination Query Processing JavaBean
Package split;
Import java. SQL .*;
Import java. util .*;
Import data. logindata;
Public class splitpage
{
Private connection conn = NULL;
Private statement stmt = NULL;
Private resultset rs = NULL;
Private resultsetmetadata rsmd = NULL;
// SQL query statement
Private string sqlstr;
// The total number of records
Public int rowcount;
// Number of logical pages divided
Public int pagecount;
// Number of records displayed on each page
Private int pagesize;
// Define the number of columns in the table
Private int columncount;
Public void initialize (string sqlstr, int pagesize)
{
This. sqlstr = sqlstr;
This. pagesize = pagesize;
Try
{
Logindata = new data. logindata ();
This. Conn = logindata. getconn ();
This. stmt = This. Conn. createstatement ();
This.rsw.this.stmt.exe cutequery (this. sqlstr );
This. rsmd = This. Rs. getmetadata ();
If (this. RS! = NULL)
{
This. Rs. Last ();
This. rowcount = This. Rs. getrow ();
This. Rs. First ();
This. columncount = This. rsmd. getcolumncount ();
This. pagecount = (this. rowcount/This. pagesize = 0 )? (This. rowcount/This. pagesize) :( this. rowcount/This. pagesize + 1 );
}
} Catch (exception ex)
{
Ex. printstacktrace ();
}
}
Public vector getpage (INT ipage)
{
Vector vdata = new vector ();
Int n = ipage;
Int m = 0;
M = (n-1) * This. pagesize + 1;
Try
{
If (this. RS! = NULL)
{
If (n! = 1)
{
This. Rs. Absolute (m );
}
For (INT I = 0; I <this. pagesize; I ++)
{
String [] sdata = new string [This. columncount];
For (Int J = 0; j <this. columncount; j ++)
{
Sdata [J] = This. Rs. getstring (J + 1 );
}
If (sdata = NULL)
{
Break;
}
Vdata. addelement (sdata );
This. Rs. Next ();
}
This. Rs. Close ();
This. stmt. Close ();
This. Conn. Close ();
}
} Catch (exception ex)
{
Ex. printstacktrace ();
}
Return vdata;
}
// Obtain the total number of pages
Public int getpagecount ()
{
Return this. pagecount;
}
// Obtain the total number of records in the data table
Public int getrowcount ()
{
Return this. rowcount;
}
}
// Display the paging query page in JSP
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "data. *" %>
<JSP: usebean id = "pages" Scope = "page" class = "split. splitpage"/>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%!
// Display the number of records per page
Int pagesize = 10;
String sqlstr = "";
// Current page
Int showpage = 1;
%>
<%
Sqlstr = "select * From userinfo order by ID ";
String strpage = NULL;
// Obtain the page to jump
Pages. initialize (sqlstr, pagesize );
Strpage = request. getparameter ("showpage ");
If (strpage = NULL ){
Showpage = 1;
} Else {
Try {
Showpage = integer. parseint (strpage );
} Catch (numberformatexception ex ){
Showpage = 1;
}
If (showpage <1 ){
Showpage = 1;
}
If (showpage> pages. getpagecount ()){
Showpage = pages. getpagecount ();
}
}
// Obtain the data set to be displayed
Vector vdata = pages. getpage (showpage );
%>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> display by page </title>
</Head>
<Body bgcolor = "# ffffff" text = "#000000">
<H1 align = center> basic personal information <Div align = center>
<Table border = "1" cellspacing = "0" cellpadding = "0" width = "80%">
<Tr>
<TH width = "20%"> NO. </Th>
<TH width = "40%"> Student ID </Th>
<TH width = "40%" type = "parmname" text = "parmname"> name </Th type = "parmname" text = "/parmname">
</Tr>
<%
For (INT I = 0; I <vdata. Size (); I ++)
{
// Display data count
String [] sdata = (string []) vdata. Get (I );
%>
<Tr>
<TD> <% = sdata [0] %> </TD>
<TD> <% = sdata [1] %> </TD>
<TD> <% = sdata [2] %> </TD>
</Tr>
<%
}
%>
</Table>
<P>
<Form action = "word_list_javabean.jsp" method = "get" target = "_ Self">
<P> total <font color = Red> <% = pages. getrowcount () %> </font> & nbsp; <% = pagesize %> entries/Page & nbsp; page <font color = Red> <% = showpage %> </font>/total <font color = Red> <% = pages. getpagecount () %> </font> page & nbsp; [<a href = "word_list_javabean.jsp? Showpage = 1 "target =" _ Self "> homepage </a>] & nbsp;
<%
// Determine whether the "Previous Page" link is to be displayed
If (showpage> 1 ){
%>
[<A href = "word_list_javabean.jsp? Showpage = showPage-1 %> "target =" _ Self "> previous page </a>] & nbsp;
<%
}
Else {
%>
[Previous Page] & nbsp;
<%
}
// Determine whether the "next page" link is displayed
If (showpage <pages. getpagecount ())
{
%>
[<A href = "word_list_javabean.jsp? Showpage = <% = showpage + 1%> "target =" _ Self "> next page </a>] & nbsp;
<%
}
Else {
%>
[Next Page] & nbsp;
<%
}
%>
[<A href = "word_list_javabean.jsp? Showpage = <% = pages. getpagecount () %> "target =" _ Self "> last page </a>] & nbsp; go
<Select name = "select">
<%
For (INT x = 1; x <= pages. getpagecount (); X ++)
{
%>
<Option value = "<% = x %>"
<%
If (showpage = x ){
Out. println ("selected ");
}
% >>><%= X %> </option>
<%
}
%>
</SELECT>
Page & nbsp;
<Input type = "Submit" name = "go" value = "Submit"/>
</P>
</Form>
</P>
</Div>
</Body>
</Html>