Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When we have JNDI address for a jdbc datasource , how we can make connection for it?

 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post.
Imagine that i have a jndi datasource like :
java:comp/env/jdbc/be
now i want to creat a connection from this datasource , how i can do that ?
can you please give me the steps ?
I want to do it in a servlet.

Thanks again.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35977
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raminaa,
1) Create an initial context. Likely this is as simple as:
InitialContext ctx = new InitialContext(), but it depends on your app server.
2) Get the datasource from the initial context. - lookup by JNDI name
3) Get a connection from the datasource. - getConnection() method.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 35977
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, there is a good example in this thread.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!