JAVA/JSP Learning Series VIII (rewrite MySQL page example)

Source: Internet
Author: User
Tags mysql stmt mysql database
js|mysql| Page One, preface

In fact, the rewritten JDBC Data-source is run in the servlet, looking for a data source through Jndi. I use the Orion trial, the site of the "Java/jsp Learning series of six (MySQL page example)," a simple rewrite.

Second, the configuration

(1) Jdbc

Need to use JDBC driver copy to [Orion]/lib directory

(2) Data-source

Add the following in the [Orion]/config/data-sources.xml file:

〈data-source

Class= "Com.evermind.sql.DriverManagerDataSource"

Name= "Mysqldbpage"

location= "Jdbc/hypersoniccoreds"

xa-location= "Jdbc/xa/hypersonicxads"

ejb-location= "Jdbc/mysqldbpage"

Connection-driver= "Org.gjt.mm.mysql.Driver"

Username= "Root"

Password= ""

Url= "Jdbc:mysql://localhost/test"

inactivity-timeout= "30"

/〉

It is to be noted that:

(1) ejb-location This "Jdbc/mysqldbpage" is the Jndi to look for.

(2) Connection-driver for JDBC Database driver

(3) URL is a URL in jdbc

(4) Username for database user name

(5) Password for user password

(6) Inactivity-timeout for database connection timeout, default is 30 seconds

For the rest of the place do not change.

Third, rewrite the following code:

<%@ page contenttype= "text/html;charset=gb2312"%>

<%@ page import= "java.sql.*, Javax.sql.DataSource, Javax.naming.InitialContext"%>

<%

Create a Jndi Lookup object

InitialContext jndi_context = new InitialContext ();

Jndi Lookup Data Source

DataSource ds = (DataSource) jndi_context.lookup ("Jdbc/mysqldbpage");

Get a data source connection

Connection conn = Ds.getconnection ();

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;

}

Get the results

stmt = Conn.createstatement ();

ResultSet Sqlrst = Stmt.executequery ("Select F1 from Test");

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 ">

&LT;TITLE&GT;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 ();

%>

Third, how to run?

See the Java/jsp Learning Series five (Jdbc-odbc page example).

Note: The MySQL database is test with a table test in the middle and a field F1 (varchar)



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.