File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes urgent ->why to follow jdbc steps for getting conncetion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "urgent ->why to follow jdbc steps for getting conncetion" Watch "urgent ->why to follow jdbc steps for getting conncetion" New topic
Author

urgent ->why to follow jdbc steps for getting conncetion

schandu999
Greenhorn

Joined: Feb 07, 2002
Posts: 9
Hi ,
Why it required to follow the normal jdbc steps for getting the connectiion as

#1
Driver d = (Driver)(Class.forName( DRIVER ).newInstance());
DriverManager.registerDriver(d);
#2
Connection conn = DriverManager.getConnection (DBURL, username, pwd);
#3
Then u get the connection and execute the SQL statements
But the same we can do this way
PLs tell me what is wrong in this way , why i am calling it wrong way coz never seen ppl doing this way

Thanks
[ Edited by Dave to format code and fix smilies ]
[ April 01, 2002: Message edited by: David O'Meara ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

The initial design for JDBC allows operations to be performed independant of the type of database being used.
In the code sample you gave, if you changed databases or drivers so that you no longer used the JDBC-ODBC bridge (because it's evil) you would have to rewrite and recompile your code.
If you have classes specific to adriver all through your code you would have to rewrite everything.
If you write your code in a generic manner then you only have to chamge a single string, and if the string is read from properties rather than being hard-coded you won't have to rewrite a single line of your code.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

as a side comment, here's how I usually do it:

Note that you only have to call Class.forName(driver), you don't have to create and maintain an instance or register that instance with the DriverManager. A call to Class.forName() will do it all for you (if you want to know more, look up static initialisers)
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

On a completely unrelated note, printing an exception is not the same as handling it, and calling System.exit(x) is dangerous. If you aren't going to handle an exception at that level, get the method to declare it and pass it up.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"champak",
You have been asked to change your name to comply to the JavaRanch naming policy multiple times.
Please change your name so that consists of at least two words, separated by a space, and that it is your full real name.
If you don't, this account will be locked.
Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
[ April 01, 2002: Message edited by: David O'Meara ]
schandu999
Greenhorn

Joined: Feb 07, 2002
Posts: 9
David
the class.forname is used for the loading the class runtime.
for example if i don'nt know the driver to be loaded at compile time then its ok to use.
But if I know before compile time driver to be used then i don't feal there is any use of that.
I can just create a new instance of the class
like
Driver d = new sun.jdbc.odbc.JdbcOdbcDriver();
instead of
DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(DRIVER);
and this code also works.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I appologise for repeating myself, but please change your name to comply to the naming standard or your account will be closed
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: urgent ->why to follow jdbc steps for getting conncetion
 
Similar Threads
No suitable driver
Problem in accessing text file using JDBC on zLinux server.
establishing connection to mssql2000
Connection to a db
BMP, Jboss, and mysql configuring