File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes connection without class.forName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "connection without class.forName" Watch "connection without class.forName" New topic

connection without class.forName

Hari babu
Ranch Hand

Joined: Jun 25, 2001
Posts: 208
Hi all,
How can i get the data base connection without using the class.forName.
Thanks in advance
Pearlo Muthukumaran
Ranch Hand

Joined: Sep 17, 2002
Posts: 79
Hi ,
You can use the following of course if you are having a App Server in place:
import javax.sql.*;
import javax.naming.*;
and within method
Hashtable ht = new Hashtable();
InitialContext ictx = new InitialContextFactory(ht);
DataSource ds = (DataSource)ictx.lookup("some_jdbc_connection_pool_in_server");
Connection cn = ds.getConnection();
then as usual whatever you want to do with connection
Hope this serves your need

InitialContext ictx
Hari babu
Ranch Hand

Joined: Jun 25, 2001
Posts: 208
I want to do this without using my application server. how do i do this ?
Any help is appreciated
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

use the DriverManager class:
Oracle example:

[ September 22, 2002: Message edited by: Jamie Robertson ]
A Sethi

Joined: Oct 11, 2005
Posts: 6

But i have seen people connecting to dataBase sucessfully in the following manner.
how is this done??

Following is the link of the code from javaranch user only who was able to access database without the DriverManager.registerDriver statement and Class.forName.

Please explain me .... how is he able to access in this fashion without DriverManager.registerDriver statement.

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The user in the thread linked imports the driver and then does the equivalent thing to Class.forName(), except that their code is now hard-coded to the Driver class. Changing database DRivers requires them to recompile their code.

If you've every seen applications like Jira (bug tracking) and a whole bunch of others including anything that accesses a database a wondered 'How do they have code that can change databases so easily?', the answer is using either Class.forName() or a DataSource to make your code independent of the dtaabase type and Driver type.

Martin Wingert

Joined: Oct 10, 2005
Posts: 16
You can also use the jdbc.drivers property to register a driver to the DriverManager e.g java -Djdbc.drivers=drivers MyProgram. Then you can skip the Class.forName("driver") since the driver will already be registered in the DriverManager.
A Sethi

Joined: Oct 11, 2005
Posts: 6
Thanks, I got it
I agree. Here's the link:
subject: connection without class.forName
It's not a secret anymore!