Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent ->why to follow jdbc steps for getting conncetion

 
schandu999
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appologise for repeating myself, but please change your name to comply to the naming standard or your account will be closed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic