We will now create a JDBC connection pool, which will be available via JNDI. Within our project, we will create an XML file and edit it to contain information about the previously mentioned pool. Finally, we will copy MySQL's JDBC driver into our project.
context.xml
, and click on Finish to continue.<?xml version="1.0" encoding="UTF-8"?> <Context> <Resource auth="Container" description="Sakila DB Connection" name="jdbc/sakila" type="javax.sql.DataSource" password="root" driverClassName="com.mysql.jdbc.Driver" maxIdle="2" maxWait="5000" validationQuery="select 1" username="root" url="jdbc:mysql://localhost:3306/sakila" maxActive="4" /> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>META-INF/context.xml</WatchedResource> </Context>
javax.sql.DataSource
, which is a Java object implementing the JDBC connection pool. This resource can be accessed by its name jdbc/sakila
by means of Java Naming and Directory Interface (JNDI) that provides an abstraction level; this allows us to modify connection parameters without having to modify the source code and recompile it. It is mandatory to complete the username and password fields with values that are appropriate for the MySQL instance being used.To obtain further information on JNDI, you can visit the Wikipedia page at http://en.wikipedia.org/wiki/Java_Naming_and_Directory_Interface.
mysql-connector-java-x.x.xx-bin.jar
, which should be placed at [PRD_HOME]/lib/jdbc
, and copy it (Ctrl + C). Then in Eclipse, we will select the node named prd5ch14 by going to WebContent | WEB-INF | lib and press Ctrl + V to paste it there.At this stage, the contents of the nodes META-INF and WEB-INF/lib should look similar to the following screenshot: