|
This article tells how to configure a MySQL data source in
Tomcat 4.1 server. This article assumes that you have
installed the proper versions of MySQL and tomcat. Here
we assume that you have installed Tomcat to C:\Tomcat. The name of web
application that we are going to configure is Test. The JNDI
name used for configuring the data source id jdbc/Test.
|
| |
System Requirements
- Apache tomcat 4.1.31
- MySQL
- Ensure that you have the following files in your lib folder.
- commons-collections.jar
- commons-dbcp-xx.jar
- commons-logging-api.jar
- commons-pool-xx.jar
- naming-common.jar
- naming-factory.jar
- naming-resources.jar
|
| |
Download and Install drivers
- Download the MySQL 3.12 JDBC driver from http://dev.mysql.com/downloads/
- Unpack the file you have downloaded.
- Copy mysql-connector-java-xxxxx.jar file into the common/lib directory of your Tomcat installation
directory. Not to your web application's lib
directory.
|
| |
Shut down Tomcat
-
Run bin/shutdown.sh or bin/shutdown.bat
to shut down Tomcat.
|
Edit server.xml
- Edit the conf/server.xml file in your Tomcat installation.
- Find the lines starting with <Host ...>
- Directly after this tag add these lines
|
| |
|
<Context
className="org.apache.catalina.core.StandardContext"
cachingAllowed="false"
charsetMapperClass="org.apache.catalina.util.CharsetMapper"
cookies="true"
crossContext="false"
debug="0"
docBase="C:\Tomcat\webapps\Test"
mapperClass="org.apache.catalina.core.StandardContextMapper"
path="/Test"
privileged="false"
reloadable="true"
swallowOutput="false"
useNaming="true"
workDir="work\Standalone\localhost\Test"
wrapperClass="org.apache.catalina.core.StandardWrapper">
<Logger
className="org.apache.catalina.logger.FileLogger"
prefix="localhost_test_log."
suffix=".txt"
timestamp="true"/>
<Resource
auth="Container"
description="Tomcat-MySQL
Test" name="jdbc/Test"
scope="Shareable"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/Test">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>*********</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/testdb?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context> |
|
| |
Where maxActive is the maximum number of database connections in pool.
Setting this value to zero means no
limit. maxIdle is the maximum number of
idle connections in pool. maxWait is the
maximum time to wait for a database
connection to become available in ms,
An Exception is thrown if this timeout
is exceeded. A value -1 means waiting
indefinitely. username and password is
the username and password to connect to
MySQL database. url is the JDBC
connection URL. Replace the values
with the correct parameters to connect
to database.
|
| |
Configure web application (web.xml
configuration).
-
Edit Test/WEB-INF/web.xml. Add the following
lines just before </web-app>
|
| |
<resource-ref>
<description>Tomcat- MySQL
Test</description>
<res-ref-name>jdbc/Test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref> |
|
| |
Test Code
- Create a jsp file to test our database connection.
|
javax.naming.Context
ctx = new javax.naming.InitialContext();
javax.sql.DataSource
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Test");
java.sql.Connection
con = ds.getConnection();
out.println("Successfully connected to MySQL
Database."); |
|
| |
Restart Tomcat
-
Run
bin/startup.sh or bin/startup.bat to start Tomcat with the new settings.
|
| |
Now you can execute your code.
Note: When starting Tomcat, if your console just vanishing
away, make sure that you don't have a context with the name
'Test' already defined. If you are getting a ServletException
'javax.servlet.ServletException: Cannot create JDBC driver of
class '' for connect URL 'null', cause: No suitable driver',
please recheck your server.xml. Still if you are unable to
connect to the database, please
contact us indicating the error messages you are getting.
|