• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable make database connection using JNDI with tomcat

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi every one
I have configured tomcat with eclipse and made an struts application. i want to connect this to database using jndi. In which xml i have to write configuration.
my application workspace is out of webapp folder of tomacat6.

thanks in advance
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html
You didn't told which version of tomcat you're using.
 
sanjay yadav yadav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I gone through given link and configured my web.xml file like

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

and there is context.xml file in project name as strutsapp/WebContent/META-INF/context.xml. and the entries are as:

<Context docBase="${CATALINA_HOME}" privileged="true" antiResourceLocking="false"
antiJarLocking="false">

<Resource name="jdbc/WareHouse"
auth="Container"
type="javax.sql.DataSource"
username="db2admin" password="password"
driverClassName="com.ibm.db2.jcc.DB2Driver"
url="jdbc:db2://localhost:50000/HONDAS2"
maxActive="10"
maxIdle="4"
/>

</Context>

and i wrote java code like

Context initContext = new InitialContext();
Context envCtx = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/WareHouse");
con = ds.getConnection();

remember application is out side the webapp folder of tomcat 6. i m running this project through eclipse

 
sanjay yadav yadav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where should i have to put the context.xml file in this case.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sanjay yadav yadav wrote:Where should i have to put the context.xml file in this case.


In META-INF folder and please quote your source code .
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, sanjay, although we really could do without the red ink!

Eclipse doesn't factor into this. You should be OK with your META-INF/context.xml file, but since you aren't placing the WAR in the Tomcat webapps folder, you need to tell Tomcat where you did put it. CATALINA_HOME is a bad location, so I'm going to make up a location inside an Eclipse project. The exact location will vary, depending on how your system is set up, but this is typical:


Copy this file as "mywebapp.xml" into CATALINA_HOME/conf/Catalina/localhost/ and Tomcat will deploy it, including the configuration of your JDBC connection pool and its JNDI resource registration. The exact name you use for your external context doesn't really matter, but since there's only room in a directory for one file of a given name, I picked "mywebapp.xml" to make it distinct.
reply
    Bookmark Topic Watch Topic
  • New Topic