Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC with JNDI

 
Baseet Ahmed
Ranch Hand
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Baseet,
Welcome to JavaRanch!

See Sun's example of getting a datasource from the JNDI.
 
Kamesh Rao
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Java Notepad Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you to both of you.
 
andre marcel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic