(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
prefix = "localhost_dbtest_log." suffix = ". txt"
TimeStamp = "true" />
2. Then fight D: / Tomcat5 / WebApps / MyApps / Web-INF / Web.xml, add the following State: (can not add)
(Note To add before , not before the
This section should be added to the resource mail / session below, as follows:
)
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);
}
%>