( 0-5 years ) Bsnl, Infosys, Tcs, IBM, HP are Hiring - Apply Here

SURESHKUMAR.NET FORUMS
Registered Member Login:
Not a member? Register today!



Welcome to the SURESHKUMAR.NET FORUMS.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.




Database connection with JNDI

        

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-06, 05:11 PM   #1 (permalink)
Junior Member
 
Join Date: Sep 2006
Posts: 29
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 4 sajeev_k is on a distinguished road
Database connection with JNDI

If we are using JNDI data source

is it mandatory to add about this in web.xml

please give an example with source

sajeev_k is offline Offline   Reply With Quote
Old 04-12-06, 06:51 PM   #2 (permalink)
Moderator
 
t_mohan's Avatar
 
Join Date: Apr 2006
Location: India
Posts: 637
Thanks: 57
Thanked 91 Times in 70 Posts
Rep Power: 20 t_mohan has much to be proud of t_mohan has much to be proud of t_mohan has much to be proud of t_mohan has much to be proud of t_mohan has much to be proud of t_mohan has much to be proud of t_mohan has much to be proud of t_mohan has much to be proud of
Re: Database connection with JNDI

Yes, if u want to get a database connection using JNDI u need to define it in the web.xml.
This is how u have to do

--resource-ref--
description--DB Connection --/description--
--res-ref-name--jdbc/somename --/res-ref-name--
--res-type--javax.sql.DataSource--/res-type--
--res-auth--Container --/res-auth--
--/resource-ref--

And also a context.xml in ur Web application META-INF folder

--Context path="/Webapp1"--
--Resource name="jdbc/somename" auth="Container" type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/urdbname" username="root" password="XXXX" maxActive="100" maxIdle="10" maxWait="10000"/--
--/Context--

This is how u have to define it

And after that write a DB connection class which creates a Connection Pool using this JNDI
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class ConnectionPool {
private static DataSource ds = null;
private ConnectionPool() {}
static {
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/somename");
} catch (NamingException ne) {
} catch (Exception e) {
}
}
public static Connection getConnection() {
Connection conn = null;
try {
conn = ds.getConnection();
} catch (SQLException se) {
} catch (Exception e) { }
return conn;
}
}

and in ur some dao class use this

conn = ConnectionPool.getConnection();
stmt = conn.createStatement();

Please use XML opening and closing tags in ur web.xml and context.xml instead of --
t_mohan is offline Offline   Reply With Quote
Reply

Tags
connection , database , jndi


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Engineer with 2-4 Years Exp - Noida sridhar Experienced Jobs 2 05-09-06 09:20 PM
5 common PHP database Problems vjsreevs PERL, PYTHON, PHP & MY SQL 0 04-09-06 03:43 PM
regarding oracle versions Madhuri ORACLE , SQL SERVER , SYBASE & Others 7 07-07-06 06:06 PM
connection with database chandra DATA STRUCTURES, C, C++, VC ++ 3 20-04-06 07:04 AM
java, jdbc, servlets raghav JAVA Technologies 11 14-03-06 09:59 AM


All times are GMT +6.5. The time now is 01:03 PM.

More Interview Questions Here...

Content Relevant URLs by vBSEO 3.3.2