To recap, there are four steps: install the driver, populate context.xml, populate web.xml, and write a program to get a connection. We saw a little bit of this configuration in a small DEMO, using Tomcat6.0 connecting to the mysql5.0.8 database as an example.

Install the driver

Download the JDBC driver for the database version you want to connect to and add it to your application’s CLASSPATH. If tomcat is used for deployment, it is best to add tomcat as well. So it was added). There should be a way not to add, if the master passes by, please give directions.

Fill in the context. The XML

So this is kind of a declaration where you define the properties that you want to connect to. My configuration is as follows:

<Context crossContext=”true” docBase=”E:/workspace/myeclipse-workspace/WebDevDemo/WebRoot” path=”/webdemo”>

<Resource name=”jdbc/EmployeeDB”

auth=”Container”

type=”javax.sql.DataSource”

username=”root”

password=””

driverClassName=”com.mysql.jdbc.Driver”

url=”jdbc:mysql://localhost:3306/employee”

maxActive=”8″

maxIdle=”4″/>

</Context>

Fill in the web. The XML. My configuration:

<resource-ref>

<res-ref-name>jdbc/EmployeeDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

Write a program to get a connection. My program:

Context ctx = null;

Context context = null;

DataSource dataSource = null;

Connection conn = null;

PreparedStatement statement = null;

ResultSet rs = null;

try {

ctx = new InitialContext();

context = (Context) ctx.lookup(“java:comp/env”);

dataSource = (DataSource)context.lookup(“jdbc/EmployeeDB”);

} catch (NamingException e) {

e.printStackTrace();

}

try {

conn = dataSource.getConnection(); // Get the connection

/ / get data

statement = conn.prepareStatement(SQL_FIND_ALL_SALARY_RECORDS);

rs = statement.executeQuery();

} catch (SQLException e) {

e.printStackTrace();

}

It is much easier to connect to the database as a configuration in Tomcat than to define all the connection properties in a class. Since the company is still using Version 5.0 of Tomcat, the author also conducted an experiment with it. There are two differences from 6.0 in operation:

(1) In the first step above, when loading the driver. For version 5.0, the driver JAR package should be placed under %CATALINA_HOME%\common\lib; For version 6.0, the driver JAR package should be placed under %CATALINA_HOME%\lib.

(2) In step 2 above, the connection is declared differently. Version 5.0 is a bit more complicated and needs to be configured as follows:

<Resource name=”jdbc/EmployeeDB” auth=”Container” type=”javax.sql.DataSource”/>

<ResourceParams name=”jdbc/EmployeeDB”>

<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/employee</value>

</parameter>

<parameter>

<name>maxActive</name>

<value>8</value>

</parameter>

<parameter>

<name>maxIdle</name>

<value>4</value>

</parameter>

</ResourceParams>

</Context>

The effect is the same, but written differently. Version 6.0 simplifies the configuration file, so it looks cleaner. Well, it looks like a software upgrade