aspose file tools*
The moose likes JDBC and the fly likes JDBC with JNDI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC with JNDI" Watch "JDBC with JNDI" New topic
Author

JDBC with JNDI

Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Hi,
I know the way of connectivity with the help JDBC drivers(Type 4).
But I want to know how to make database connection in Java(JDBC)
with the help of DataSource and JNDI.

If possible, pls give some code example.


Thanks in advance.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Baseet,
Welcome to JavaRanch!

See Sun's example of getting a datasource from the JNDI.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kamesh Rao
Ranch Hand

Joined: Dec 24, 2006
Posts: 35
For this you need to do two things

1. Configure the datasource with a unique name(JNDI name).
2. Write client side code to lookup for the datasource by the name using JNDI API

-->Configuring datasource varies from one server to the other. You have to refer to your server documentation for that.

-->JNDI is a platform independent way for looking up objects on a network. For this we need to obtain the InitialContext. Obtaining InitialContext differs from where we are using it for lookup.
That is if we are looking up a datasource object that are part of the same J2EE application then we can create the InitialContext as below

Context ctx=new InitialContext();

If we are looking up a datasource from a client application not part of the J2EE application then we need to use the below constructor

Context ctx=new InitalContext(HashTable<?,?> environment);

The HashTable entry passed should provide 4 entries

1.Context.INITIAL_CONTEXT_FACTORY -- specifies which initial context factory to use when creating InitialContext.
2.Context.PROVIDER_URL -- The url of the server where the datasource is configured
3.Context.SECURITY_PRINCIPAL -- The username that will be used to authenticate the application client
4.Context.SECURITY_CREDENTIAL -- The password that will be used to authenticate the application client

Once the context object is obtained you can look up the datasource object by its unique name which was configured in step 1 as follows

DataSource ds=(DataSource)ctx.lookup("datasourcename");

Connection con=ds.getConnection();

Hope it helps
Baseet Ahmed
Ranch Hand

Joined: Dec 18, 2006
Posts: 224
Hi,
Thank you to both of you.
andre marcel
Greenhorn

Joined: Jan 12, 2007
Posts: 3
thank you kamesh for your lengthy reply however I have the same problem with creating a data source. you mentioned the first step was to configure the data source with a unique name(JNDI name). can you explain how to do that please?
thanks in advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC with JNDI