I. Introduction
In fact, the rewritten JDBC Data-Source is running in the servlet, looking for the data source via JNDI. I use Orion to try this site "Java / JSP learning series (MySQL floppage example)" simply rewritten.
Second, the configuration
(1) JDBC
Need to use the JDBC driver COPY to [Orion] / lib directory
(2) Data-Source
Add below in the [Orion] /config/data-sources.xml file:
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" /> have to be aware of is: (1) EJB-LOCATION This "JDBC / MySQLDBPAGE" later is JNDI to find. (2) Connection-Driver drives the JDBC database (3) URL is the URL in JDBC (4) Username is the database username (5) Password is the user password (6) INACTIVITY-TIMEOUT for database connection timeout, default is 30 seconds Do not change for other places. Third, the changed code is as follows: <% @ 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; / / One page shows the number of records INT INTROWCOUNT; // Total number of records INT INTPAGECUNT; / / Total number of pages INT INTPAGE; // Waiting page number Java.lang.string strpage; INT I; / / Set the number of records displayed INTPAGESIZE = 2; / / Get the page number to be displayed StrPage = Request.getParameter ("Page"); IF (strpage == null) { // Indicates that there is no Page this parameter in querystring, and the first page is displayed. INTPAGE = 1; } else { / / Convert the string to integer INTPAGE = java.lang.integer.parseint (STRPAGE); IF (INTPAGE <1) INTPAGE = 1;} // got the answer STMT = conn.createstatement (); ResultSet Sqlrst = Stmt.executeQuery ("SELECT F1 from Test"); // Get the total number of records Sqlrst.last (); introwcount = sqlrst.getrow (); // Mode total page INTPAGECOUNT = (Introwcount INTPAGESIZE-1) / INTPAGESIZE // Adjust the page number to be displayed IF (INTPAGE> INTPAGECOUNT) INTPAGE = INTPAGECOUNT; %>
hEAD>
Name TH>
TR> <% IF (INTPAGECUNT> 0) { // Position the record pointer to the first record of the page to be displayed Sqlrst.absolute (INTPAGE-1) * INTPAGESIZE 1); //Display Data i = 0; While (i |
---|
<% = sqlrst.getstring (1)%> td> TR> <% SQLRST.NEXT (); i ; } } %> TABLE> No. <% = intPage%> page a total of <% = INTPAGECOUNT%> page <% IF (INTPAGE <% IF (INTPAGE> 1) {%> Previous <%}%> body> html> <% // Close the result set Sqlrst.close (); %> Third, how can I run? See the "JAVA / JSP Learning Series" five (JDBC-ODBC flip examples). Note: MySQL database is TEST, there is a table test in the middle, there is a field F1 (varchar)
转载请注明原文地址:https://www.9cbs.com/read-19788.html New Post(0)
|