Java/jsp Learning Series VI (MySQL page example) _jsp programming
Last Update:2017-01-18
Source: Internet
Author: User
Pre-run preparation
Download MySQL JDBC driver (a jar file) and load it in Classpath (method see "One of the Java/jsp Learning Series (JDK installation)")
(If not found, please download from this site)
Build a MySQL database test
There is a table in the database: note, field: name (varchar)
Second, download, install
<%@ page contenttype= "text/html;charset=gb2312"%>
<% 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 parameter in QueryString, and the first page of data is displayed at this time
Intpage = 1;
} else{
Converts a string to an integral type
Intpage = Java.lang.Integer.parseInt (strpage);
if (intpage<1) intpage = 1;
}
Load JDBC Driver
Class.forName ("Org.gjt.mm.mysql.Driver"). newinstance ();
Connecting to a database
sqlcon= java.sql.DriverManager.getConnection ("Jdbc:mysql://localhost/test");
To create a statement object
sqlstmt = Sqlcon.createstatement (java.sql.resultset.type_scroll_insensitive,java.sql.resultset.concur_read_only) ; Execute SQL statement
strSQL = "SELECT name from note";
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 operation Routines-data paging display-JDBC 2.0-mysql</title>
<body>
<table border= "1" cellspacing= "0" cellpadding= "0" >
<tr>
<th> name </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>
</tr>
<% Sqlrst.next ();
i++;
}
}
%>
</table>
Page <%=intPage%> Total <%=intPageCount%> page
<%if (intpage<intpagecount) {%><a href= "mysqlpage.jsp?page=<%=intpage+1%>" > next page </a>< %}%>
<%if (intpage>1) {%><a href= "mysqlpage.jsp?page=<%=intpage-1%>" > Prev </a><%}%>
</body>
<%
Close result set
Sqlrst.close ();
Close an SQL statement object
Sqlstmt.close ();
Close Database
Sqlcon.close ();
%>
Third, how to run?
See the Java/jsp Learning Series five (Jdbc-odbc page example).