This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes JNDI Connection using struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JNDI Connection using struts" Watch "JNDI Connection using struts" New topic
Author

JNDI Connection using struts

Jayanthi Sampath
Greenhorn

Joined: Jun 26, 2008
Posts: 6
Can anyone sugges me in connecting sql database by creating jndi.

Anticipating
Siddharth Naik
Ranch Hand

Joined: Apr 09, 2006
Posts: 35
(1) Put JDBC driver to appropriate server directory. For Tomcat put it under: Tomcat/commons/lib directory

(2) Create context.xml under META-INF directory of your project with following code. Replace values specific to your environment including driverClassName of your database.



You can also add other parameters like maxActive, maxWait etc. Do google search for context.xml and you will find details.

(3) Use the following code for JNDI lookup



Use conn object from above code for creating Statement or preferably PreparedStatement to execute the query.
[ July 07, 2008: Message edited by: Siddharth Naik ]

Thanks<br />Sid
Robert Darling
Ranch Hand

Joined: Jun 21, 2008
Posts: 100

hi i was wondering if you could explain the line
Siddharth Naik
Ranch Hand

Joined: Apr 09, 2006
Posts: 35
java:comp/env is the standard J2EE prefix used to lookup resources via JNDI.
Jayanthi Sampath
Greenhorn

Joined: Jun 26, 2008
Posts: 6
Hi,

Thanks for the immediate response. When i promote this in production server there they will create a jndi using tomcat admin. in context path i cant specify the username and password as i dont know and its not safe. Can you guide me on this..

Regards
Jayanthi


Originally posted by Siddharth Naik:
(1) Put JDBC driver to appropriate server directory. For Tomcat put it under: Tomcat/commons/lib directory

(2) Create context.xml under META-INF directory of your project with following code. Replace values specific to your environment including driverClassName of your database.

<blockquote>code:
<pre name="code" class="core">
<Context path="/myapplicationroot">
<Resource name="jdbc/mydatabasename"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
username="myusername"
password="mypassword"
url="jdbc:mysql://localhost:3306/mydatabasename"/>
</Context>
</pre>


You can also add other parameters like maxActive, maxWait etc. Do google search for context.xml and you will find details.

(3) Use the following code for JNDI lookup

<blockquote>code:
<pre name="code" class="core">
Context initCtx = new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/mydatbasename");
Connection conn = ds.getConnection();
</pre>
</blockquote>

Use conn object from above code for creating Statement or preferably PreparedStatement to execute the query.

[ July 07, 2008: Message edited by: Siddharth Naik ]
</blockquote>
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

in context path i cant specify the username and password as i dont know and its not safe.


I guess , they are not supposed to give you access to main DB server, so you must request then a DB dump on your local machine and access it for development !!


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Jayanthi Sampath
Greenhorn

Joined: Jun 26, 2008
Posts: 6
Its working in development. What i should do when i promote this. i dont know the username /psswd of production server. They cant specify it in my context path as its not safe.

help needed!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JNDI Connection using struts
 
Similar Threads
JTA objects
WA #1.....word association
lookup meaning?
ejb
J2EE technology would allow the system to interact with the LDAP server?