This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Database Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database Connection" Watch "Database Connection" New topic
Author

Database Connection

Nimish Patel
Ranch Hand

Joined: Jun 29, 2005
Posts: 84
Hi,
Have a good day...
we can implement connection using two ways..
1)first, jndi - initial context
2)second , class.forname()-driver manager

in these ways , which is good way..and what is the diffrence between this ?
when implement jndi or driver manager... ?
please clarify this.

Thanks in anticipation.

rgds,
Nimish
[ September 02, 2005: Message edited by: Nimish Patel ]
Masoud Kalali
Author
Ranch Hand

Joined: Jul 08, 2004
Posts: 531

Hi
Using the manager to get the connection is hard code way
and using jndi gives you the ability to change the connection properties in deployment .
using manager make you coupled within the connection you made , and in using jndi you are not.
another thing :
I used jndi and datasources while i need the conection to be managed by container and i need connection pooling.
I think its another benefits of using container managed (jndi based) connection management than plain jdbc one.
hope it helps


Masoud Kalali
Software Engineer - My Weblog - GlassFish Security
Sirish Kumar Gongal Reddy
Ranch Hand

Joined: Oct 25, 2004
Posts: 109
Hi,
Obtaining the Connection from JNDI:
1)That Connection is a Dynamic connection.
2)In JNDI we defined some connection previously (Connectionpool) from here we are obtaining the predefined connection.
3)High performance.
Obtaining the Connection from DriverManger:
1) Is a static connection.
2) We can create our own connection object here.
3)Poor performance for this connection.
Hope that some how cleare.
Regards,
G Sirish Reddy.,
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Get accustomed to obtaining a DataSource from a JNDI tree and using it to obtain your Connection. This is the way to write J2EE-compliant apps.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Dong Hoang
Greenhorn

Joined: May 26, 2005
Posts: 4
I am using Datasource to get connection to DB2. When would I need to use ID and password with getConnection? Can I getConnection without ID and password?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
No password is needed to get a Connection as the password is supplied on creation of the connection pool on server startup. Each instance in the pool will maintain a connection to the DB. When you look up a data source via JNDI and access a database connection, you get a stub for the Connection instance in the server process.
Dong Hoang
Greenhorn

Joined: May 26, 2005
Posts: 4
Does this mean I define a default ID and password for DB2 in the server?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Yes, each application server its own way of configuring the username and password.
Dong Hoang
Greenhorn

Joined: May 26, 2005
Posts: 4
What if I want DB2 to use the ID and password of the user that login from a login page? Can the default ID and password in the server be overwritten in my application?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

Originally posted by Dong Hoang:
What if I want DB2 to use the ID and password of the user that login from a login page? Can the default ID and password in the server be overwritten in my application?

You can't use a custom user id and password when using the DataSource from the app server. Typically in a web app, you just authenticate the user and use an "application id" for the database access.


[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
Don Hoang
Greenhorn

Joined: Jun 15, 2005
Posts: 6
I am sure what you meant by "application id" for database access. How do I get this application id?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

Don,
An "application id" is just a regular database id. You create a user for the database, say "myAppId." Then you use that id with the datasource.

The idea is that if a user is in your application to execute code against the datasource, he/she has already been authenticated.
sri subha
Greenhorn

Joined: Feb 27, 2006
Posts: 2
i want to know how making use of jndi and datasource gives u flexibility of changing the properties and datasource?

please explain.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

Sri,
Welcome to JavaRanch! Note that it is ok to ask a new question in a new thread. No need to bump old threads.

Using JNDI to get the datasource allows you to keep all the datasource's information on the server. In this way, it is completely independent of the code. You just change the properties through the server's interface and the code doesn't even have to know that it changed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database Connection
 
Similar Threads
Differences between Connection Pool managers in Weblogic 6.0
Database connection shouldnt be in Servlet?
JDBC and DataSource
multithreading in struts
Refreshing Connection pool