JSP Learning--Comprehensive analysis of JDBC (7)

Source: Internet
Author: User
Tags web database
JS How to implement the paging display in the JSP?

Paging display is a common problem in Web database application, when the user's query results far exceed the computer screen display ability, how can we reasonably present the data to the user? The answer is that the database pagination display, can perfect solve the above problem. The following is a common example of database paging operations, for any database platform paging function has a good reference meaning.

<%@ page contenttype= "Text/html;charset=8859_1"%>
<%
Variable declaration
Java.sql.Connection Sqlcon; Database Connection objects
Java.sql.Statement sqlstmt; SQL Statement Object
Java.sql.ResultSet Sqlrst; Result set Object

Java.lang.String Strcon; Database connection string
Java.lang.String strSQL; SQL statement

int intpagesize; Number of records displayed on one page
int introwcount; Total Records
int intpagecount; Total pages
int intpage; Page number to display
Java.lang.String strpage;

int i;

Set the number of records to display on a page
Intpagesize = 2;

Get the number to display
Strpage = request.getparameter ("page");
if (strpage==null) {//indicates that there is no page this parameter in QueryString, the first page data is now displayed
Intpage = 1;
}
else{//converts a string to an integral type
Intpage = Java.lang.Integer.parseInt (strpage);
if (intpage<1) intpage = 1;
}

Load JDBC Driver
Java.sql.DriverManager.registerDriver (New Oracle.jdbc.driver.OracleDriver ());

To set the database connection string
Strcon = "Jdbc:oracle:thin: @linux: 1521:ora4cweb";

Connecting to a database
Sqlcon = Java.sql.DriverManager.getConnection (Strcon, "Hzq", "hzq");

Create a read-only SQL statement object that can be scrolled
sqlstmt = Sqlcon.createstatement (java.sql.resultset.type_scroll_insensitive,java.sql.resultset.concur_read_only) ;

Preparing SQL statements
strSQL = "Select Name,age from Test";

Execute the SQL statement and get the result set
Sqlrst = Sqlstmt.executequery (strSQL);

Get Total Records
Sqlrst.last ();
Introwcount = Sqlrst.getrow ();

Count Total Pages
Intpagecount = (introwcount+intpagesize-1)/intpagesize;

Adjust the number of pages to be displayed
if (intpage>intpagecount) intpage = Intpagecount;
%>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> JSP Database Action routines-data paging display-JDBC 2.0-oracle </title>

<body>

<table border= "1" cellspacing= "0" cellpadding= "0" >
<tr>
<th> name </th>
<th> Age </th>
</tr>

<%
if (intpagecount>0) {
Position the record pointer over the first record of the page you want to display
Sqlrst.absolute ((intPage-1) * intpagesize + 1);

Display data
i = 0;
while (I<intpagesize &&!sqlrst.isafterlast ()) {
%>
<tr>
<td> <%=sqlrst.getstring (1)%> </td>
<td> <%=sqlrst.getstring (2)%> </td>
</tr>
<%
Sqlrst.next ();
i++;
}
}
%>

</table>

First <%=intPage%> page Total <%=intPageCount%> page
<%if (intpage<intpagecount) {%><a href= "jdbc20-oracle.jsp?page= <%=intPage+1%>" > next page
</a> <%}%> <%if (intpage>1) {%><a href= "jdbc20-oracle.jsp?page= <%=intPage-1%>" > prev </a> <%}%>

</body>

<%
Close result set
Sqlrst.close ();

Close an SQL statement object
Sqlstmt.close ();

Close Database
Sqlcon.close ();
%>

Comprehensive analysis of JDBC




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.