This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable make database connection using JNDI with tomcat

 
sanjay yadav yadav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
 
sanjay yadav yadav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Where should i have to put the context.xml file in this case.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 .
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic