I believe everyone is familiar with Tomcat. As a free and powerful java web server, it has been favored by many java enthusiasts. The latest version of tomcat5 supports servlet2.4 and jsp2.0. Today I will use Tomcat5 and Ms sqlserver 000 Let’s start the database connection pool configuration journey together.
Preparation required
1. The version of jdk I use is 1.4.01
2. Tomcat 5 I am using the 5.0.16 version download address: http://jakarta.apache.org/site/binindex.cgi
3. Mssql server 2000 database
4. The official jdbc driver of Mssql server 2000 can be downloaded for free from Microsoft's official website.
After installing the above software, enter the actual configuration:)
1. Find the installation directory of jdbc, and change the msbase. jar, along with the three files mssqlserver.jar and msutil.jar, copy them to $CATALINA_HOME/common/lib/ ($CATALINA_HOME represents the installation directory of your tomcat5).
2. Use a text editor. I am using editplus (she is My dearest friend) Open the $CATALINA_HOME/conf/server.xml file, find the place where context is configured, and paste the following code into the file
<Context path="/DBTest" docBase="D:rautinee workdb"
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.BasicDataSourceFactory</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 password of sa in my local database is empty. The database uses Northwind. My directory name is DBTest. His directory is D:rautinee workdb.
Open the web.xml file under DBTest. 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 completed. The next step is to write two files to test whether the connection is successful.
Here I used http://jakarta.apache.org. The above example
is first 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.executeQuery("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</h2>
Foo <%= tst.getFoo() %><br/>
Bar <%= tst.getBar() %>
</body>
</html>
'www.downcodes.com
compiles and runs. If nothing goes wrong, a record should be retrieved.
What is displayed in my IE is
Ms sql server 2000 java search results
FooVINET
Bar 10248
ok, prepared successfully!
Reference documentation:
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html has a tutorial on connecting mysql and oracle8i. Friends who are interested can check it out.
Author Haizai email:[email protected] http://www.tryitsoft.com