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>