What is JNDI resource? in simplest term the name that we will be using to identify resource(in our case connection JDBC connection pool to Oracle)
Resource creation
We will create global resource by adding its details in server.xml in tomcat.
$CATALINA_HOME/conf/server.xml
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.DataSource" username="<<DB_user>" password="<<DB_pass>>" driverClassName="oracle.jdbc.OracleDriver" factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" url="jdbc:oracle:thin:@localhost:1521:XE" maxActive="20" maxIdle="10" maxWait="-1" />
Config is simple as it looks
JNDI Name for resource will be : jdbc/myDB
username: database user name
password: database user password
driverClassName : In our case its Oracle dirvers
url: connection url to db server.
Context reference
Reference of resource will be specified in context.xml
$CATALINA_HOME/conf/context.xml
<ResourceLink name="jdbc/myDB"
global="jdbc/myDB"
type="javax.sql.DataSource"/>
Environment:
Tomcat 6
Oracle 10G Express edition
Working code
- Create user tomcat in Oracle with password tomcat
- Execute SQL to create table in tomcat schema
- Replace (or copy content of) server.xml & context.xml from config folder
- Copy jstl libs from lib folder
- Download and copy ojdbc6.jar from Oracle
- Copy JNDI folder in webapps
Ref:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html
http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
http://www.datadisk.co.uk/html_docs/java_app/tomcat6/tomcat6_jdbc.htm