Configuring a database (Oracle) connection pool in Tomcat5Tomcat4

xiaoxiao2021-03-06  43

(My collection will have a comment, if the original author doesn't like it, I can let me know!)

First, build a table in Oracle, SQL is as follows:

Create Table Book (Book_code Char (5) Not Null,

Cost Number (7, 2));

Insert a few records:

INSERT INTO BOOK VALUES (B0020, 13.50);

Insert INTO BOOK VALUES (B0220, 12.00);

......

The database is ready!

My Tomcat installation path is D: / Tomcat5, build its own web application directory in the WebApps directory of the D: /. The doll is named: MyApps. Then establish a web-inflicide in your own directory. It is recommended to come from D: / Tomcat5 / WebApps / JSP-EXAMPLES / to come over, which is configured, especially JSP2.0. New State East. The current directory structure should be:

D: / Tomcat5

| ------ / WebApps

| ------- / MyApps

| ------ / Web-INF

Now start to prepare for DBCP. Put the Database's JDBC you want to configure under d: / tomcat5 / common / lib, pay attention to the ".jar" file, if ".zip" file is changed directly to .jar. This example is a JDBC with an Oracle, even renamed classes111.zip to classes111.jar.

Let's start configuring an XML file:

1. Configuration for Server.xml:

Use (your habit) text editor to open d: /tomcat5/conf/server.xml, find to change it below, and add Oracle Setting information.

> TRUE ">

prefix = "localhost_dbtest_log." suffix = ". txt"

TimeStamp = "true" />

factory

org.apache.commons.dbcp.basicDataSourceFactory

driverclassname

Oracle.jdbc.driver.Oracledriver

URL

JDBC: Oracle: Thin: @ 127.0.0.1: 1521: Accp

Username

scott

Password

Tiger

maxactive

20

maxidle

10

maxwait

10000

2. Then fight D: / Tomcat5 / WebApps / MyApps / Web-INF / Web.xml, add the following State: (can not add)

Oracle DataSource Example

jdbc / myoracle

javax.sql.datasource

container

(Note To add before , not before the , Start Tomcat error, that is, the following format :

Oracle DataSource Example

JDBC / PDM

javax.sql.datasource

container

This section should be added to the resource mail / session below, as follows:

mail / session

javax.mail.session

container

Oracle DataSource Example

JDBC / PDM

javax.sql.datasource

container

)

OK! The configuration work has been completed. Here is to write a JSP code to get a test.

Write a Test.jsp in d: / tomcat5 / webapps / myapps /, the content is as follows:

<% @ Page Import = "javax.naming.context"%>

<% @ Page Import = "javax.sql.datasource"%>

<% @ page import = "javax.naming.initialcontext"%>

<% @ Page Import = "java.sql. *"%>

<%

DataSource DS = NULL;

Try {

CONTEXT INITCTX = New InitialContext ();

Context envctx = (context) INITCTX.LOOKUP ("Java: Comp / ENV");

DS = (DataSource) Envctx.lookup ("JDBC / Myoracle");

IF (ds! = null) {

Out.println ("Connection IS OK!");

CONNECTION CN = ds.getConnection ();

IF (cn! = null) {

Out.println ("CN IS OK!");

Statement Stmt = cn.createStatement ();

ResultSet RST = Stmt.executeQuery ("Select * from book");

Out.println ("

RST IS OK!" RST.NEXT ());

While (Rst.next ()) {

Out.println ("

book_code:" RST.GETSTRING (1));

}

Cn.close ();

} else {

Out.println ("RST Fail!");

}

}

Else

Out.println ("fail!");

} catch (exception ne) {Out.println (ne);

}

%>

转载请注明原文地址:https://www.9cbs.com/read-56803.html

New Post(0)