Tomcat is familiar to everyone. As a free and powerful java web server, Tomcat is favored by many java enthusiasts. The latest version of Tomcat 5 supports servlet2.4 and jsp2.0, today, I will use Tomcat5 and Ms sqlserver 000 to start the database connection pool configuration tour.
Required preparation
1. jdk my version 1.4.01
2, Tomcat 5 I use 5.0.16 version: http://jakarta.apache.org/site/binindex.cgi
3. Mssql server 2000 database
4. Official jdbc driver of Mssql server 2000 can be downloaded from Microsoft's official website for free.
After the above software is installed, the configuration will go into practice :)
1. Find the jdbc installation directory and put msbase under the lib directory. jar and mssqlserver. jar, msutil. copy the Three jar files to $ CATALINA_HOME/common/lib/($ CATALINA_HOME indicates your Tomcat 5 installation directory)
2. In the text editor, I used editplus (she is my favorite) to open the $ CATALINA_HOME/conf/server. xml file, locate the configuration context, and run the following code:
Paste it into a file
<Context path = "/DBTest" docBase = "D: \ rautinee work \ db \"
Debug = "5" reloadable = "true" crossContext = "true">
<Logger className = "org. apache. catalina. logger. FileLogger"
Prefix = "localhost_DBTest_log." suffix = ". txt"
Timestamp = "true"/>
<Resource name = "jdbc/TestDB"
Auth = "Container"
Type = "javax. SQL. DataSource"/>
<ResourceParams name = "jdbc/TestDB">
<Parameter>
<Name> factory </name>
<Value> org. apache. commons. dbcp. basicperformancefactory </value>
</Parameter>
<! -- Maximum number of dB connections in pool. Make sure you
Configure your mysqld max_connections large enough to handle
All of your db connections. Set to 0 for no limit.
-->
<Parameter>
<Name> maxActive </name>
<Value> 100 </value>
</Parameter>
<! -- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
<Parameter>
<Name> maxIdle </name>
<Value> 30 </value>
</Parameter>
<! -- Maximum time to wait for a dB connection to become available
In MS, in this example 10 seconds. An Exception is thrown if
This timeout is exceeded. Set to-1 to wait indefinitely.
-->
<Parameter>
<Name> maxWait </name>
<Value> 10000 </value>
</Parameter>
<! -- MSSQLserver dB username and password for dB connections -->
<Parameter>
<Name> username </name>
<Value> sa </value>
</Parameter>
<Parameter>
<Name> password </name>
<Value> </value>
</Parameter>
<! -- Class name for mssqlserver JDBC driver -->
<Parameter>
<Name> driverClassName </name>
<Value> com. microsoft. jdbc. sqlserver. SQLServerDriver </value>
</Parameter>
<! -- The JDBC connection url for connecting to your mssqlserver dB. -->
<Parameter>
<Name> url </name>
<Value> jdbc: microsoft: sqlserver: // localhost: 1433; databasename = Northwind </value>
</Parameter>
</ResourceParams>
</Context>
Note: The sa password of my local database is blank. The database uses Northwind and my directory name is DBTest. Its Directory is D: \ rautinee work \ db \
Open the web. xml file under DBTest and replace the original content with the following code.
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE web-app PUBLIC
"-// Sun Microsystems, Inc. // DTD Web Application 2.3 // EN"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
<Description> MSSql server Test App </description>
<Resource-ref>
<Description> DB Connection </description>
<Res-ref-name> jdbc/TestDB </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
</Web-app>
OK. The configuration is complete. The following task is to write two files to test whether the connection is successful.
Here I use the example above in http://jakarta.apache.org
The first is the bean file.
Package foo;
Import javax. naming .*;
Import javax. SQL .*;
Import java. SQL .*;
Public class DBTest {
String foo = "Not Connected ";
Int bar =-1;
Public void init (){
Try {
Context ctx = new InitialContext ();
If (ctx = null)
Throw new Exception ("Boom-No Context ");
DataSource ds = (DataSource) ctx. lookup ("java: comp/env/jdbc/TestDB ");
If (ds! = Null ){
Connection conn = ds. getConnection ();
If (conn! = Null ){
Foo = "Got Connection" + conn. toString ();
Statement stmt = conn. createStatement ();
ResultSet rst =
Stmt.exe cuteQuery ("select * from orders ");
If (rst. next ()){
Foo = rst. getString ("CustomerID ");
Bar = rst. getInt ("OrderID ");
}
Conn. close ();
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public String getFoo () {return foo ;}
Public int getBar () {return bar ;}
}
Then the index. jsp file
<Html>
<Head>
<Title> DB Test </title>
</Head>
<Body>
<%
Foo. DBTest tst = new foo. DBTest ();
Tst. init ();
%>
<H2> Ms SQL server 2000 java search Results Foo <% = tst. getFoo () %> <br/>
Bar <% = tst. getBar () %>
</Body>
</Html>
'Www .knowsky.com
Compile and run the program. If no exception occurs, you should retrieve a record,
What I show in ie is:
MS SQL server 2000 java search Results
Foo VINET
Bar 10248
OK. Preparation successful!
Reference:
Http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html above mysql and oracle8i connection tutorial, interested friends can go up to have a look.
Author Hai Zi email: rautinee@21cn.com http://www.tryitsoft.com