07
Sep
09

DB2 Datasource configuration in Tomcat 6


This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.

This page describes the process to get Tomcat 6 configured with a IBM DB2 Data source using a type 4 JDBC driver.

Requirements

Step 1

Ensure you have the IBM JDBC Drivers installed in your tomcat lib directory. See my other post. Next you need to create a basic hello World servlet. Do that now and return to continue step 2

Step 2

At the very end of web.xml

	<resource-ref>
		<res-ref-name>/jdbc/TESTShared</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
        </resource-ref>

Step 3

In your tomcat “conf” folder modify the context.xml and insert the following

	<ResourceLink name="jdbc/TESTShared" global="jdbc/TEST" type="javax.sql.DataSource"/>

Step 4

In your tomcat “conf” folder modify the server.xml and add the following in the GlobalNamingResources section.

		<Resource auth="Container"
		driverClassName="com.ibm.db2.jcc.DB2Driver"
		name="jdbc/TEST"
		password="xxxx"
		username="xxxx"
		type="javax.sql.DataSource"
		url="jdbc:db2://hostname:port/TEST"/>

The hostname and port are custom to your database installation. For this example we used the TEST database. But you can change this to your specific database name. Just be sure to replace it everywhere you see TEST in this page.

Step 5

Add the following to the helloWorld servet.

		System.out.println("trying to get connection...");

		Context initCtx = new InitialContext();
		Context envCtx = (Context) initCtx.lookup("java:comp/env");
		  DataSource ds = (DataSource) envCtx.lookup("jdbc/TESTShared");
		  Connection c = ds.getConnection();

		System.out.println(c);
		c.close();

Thats all

At this point you can save the source code for the test servlet and verify that you have database connectivity. By opening up a browser and navigating to the servlet. It should print out a the connection object to the standard out.

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 74 other followers

September 2009
S M T W T F S
« Aug   Oct »
 12345
6789101112
13141516171819
20212223242526
27282930  

Blog Stats

  • 801,304 hits

%d bloggers like this: