Environment:
Jdk1.4 + Tomcat 5
I. environment variables:
Path: C:/j2sdk1.4.2/bin;
Java_home: C:/j2sdk1.4.2/
Note: Only the two.
II. JDBC driver
Copy the corresponding JDBC driver to tomcat5/common/lib
Three. Jar files in the SQL Server 2000 (or MySQL) for JDBC/lib/directory:
Msbase. Jar
MSSQLServer. Jar
Msutil. Jar
Iii. virtual directory and Connection Pool
Suppose you want to create your own virtual directory in D:/jsgtest
/Myjsp
Assume that the connection pool JNDI name is AAA.
Assume that the connection database is chengji (Table chengji in the test database)
In
Tomcat5/CONF/Catalina/localhost
Directory to create a file:
Myjsp. xml
The content is as follows:
<Context crosscontext = "true" displayname = "myjsp" docbase = "D:/jsgtest" Path = "/myjsp" reloadable = "true">
<Resource Name = "AAA" type = "javax. SQL. datasource"/>
<Resourceparams name = "AAA">
<Parameter>
<Name> URL </Name>
<Value> JDBC: mysql: // localhost: 3306/test </value>
</Parameter>
<Parameter>
<Name> maxactive </Name>
<Value> 4 </value>
</Parameter>
<Parameter>
<Name> maxwait </Name>
<Value> 5000 </value>
</Parameter>
<Parameter>
<Name> driverclassname </Name>
<! -- Old driver <value> org. gjt. Mm. MySQL. Driver </value> -->
<Value> com. MySQL. JDBC. Driver </value>
</Parameter>
<Parameter>
<Name> username </Name>
<Value> root </value>
</Parameter>
<Parameter>
<Name> password </Name>
<Value> </value>
</Parameter>
<Parameter>
<Name> maxidle </Name>
<Value> 2 </value>
</Parameter>
</Resourceparams>
</Context>
Test:
Create the file test. jsp in the Directory D:/jsgtest:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<%
Connection conn = NULL;
Context initctx = new initialcontext ();
If (initctx = NULL)
Throw new exception ("context cannot be obtained! ");
Context CTX = (context) initctx. Lookup ("Java: COMP/ENV"); // obtain the connection pool object
Object OBJ = (object) CTX. Lookup ("AAA"); // type conversion
Javax. SQL. datasource DS = (javax. SQL. datasource) OBJ;
Conn = Ds. getconnection ();
Statement stmt = conn. createstatement ();
String strsql = "select * From chengji ";
Resultset rs = stmt.exe cutequery (strsql );
While (Rs. Next ()){
Out. println (Rs. getstring (1 ));
}
Rs. Close ();
Stmt. Close ();
Conn. Close ();
Out. println ("connection pool test succeeded ");
%>
Test path:
HTTP: /localhost: 8080/myjsp/test. jsp