• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JNDI Connection using struts

 
Jayanthi Sampath
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone sugges me in connecting sql database by creating jndi.

Anticipating
 
Siddharth Naik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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 ]
 
Robert Darling
Ranch Hand
Posts: 100
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i was wondering if you could explain the line
 
Siddharth Naik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java:comp/env is the standard J2EE prefix used to lookup resources via JNDI.
 
Jayanthi Sampath
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Jayanthi Sampath
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic