How to configure a Data Source in Tomcat 5.5.x or 6 for a web application
Incase you have any difficulty in configuration please
contact us.
In this tutorial you will learn how to configure a datasource [to obtain a database connection]
in Tomcat 5.5.x or Tomcat 6.
Step 1: Let us assume Tomcat is installed in
C:\tomcat. Navigate to
C:\tomcat\conf
and open
server.xml.
Step 2: Configure a datasource for a web application with context
bookstore
and docBase as bookstore [in this example the docBase is
c:\tomcat\webapps\bookstore]
as in the screen shot below. The configuration is under the desired
Host.
MySQL is considered for the example. The database instance name is
bookstoredb.
The datasource name is configured as
jdbc/datasourcename which will be used in web.xml [explained
in next step]
Step 3: Now open
web.xml under
C:\tomcat\webapps\bookstore\WEB-INF.
Place a reference to the Datasource as highlighted in the screen shot. The
res-ref-name
should be matching the Resource name provided in the server.xml in step 2.
Step 4: Below snippet shows how to obtain Connection in your Java code.