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

Loading JDBC driver

 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically JDBC drivers are loaded as:
e.g.

Why is this so?
Why do we need to register the driver?
Why dont we do it as


Is it because the drivers reside on the database or what?

What if we do it with Class.forName and the driver name mentioned is not present?
Would it use some other driver or just throw a class not found exception?


 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we use:

We must recompile the code to change fro mone database to another. OK, if we use:

we have to do the same, but if we swap the hardcoded value for a variable read in from an external source we don't.

All driver implementations are required to have a static block of which will be run when the class is loaded. In this block the driver will regiser with the DriverManager to tell it what JDBC url the driver supports. Later when you write:

the DriverManager can check what driver has registered itself to handle requests to a url with this syntax.


What if we do it with Class.forName and the driver name mentioned is not present?
Would it use some other driver or just throw a class not found exception?

Try it and see
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul thanks!

I have definitely have more clarity with respect to why one should use the Class.forName() method now.
It will keep the code flexible and portable with respect to different databases by picking up the driver class name from a property file or something.

Though I am not still comfortable with the part "registering with the dirver manager".
I will be reading the link provided and lets see how it goes.

Thanks a lot again.

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.

I thought I'd also mention that JavaRanch has a bunch of JDBC FAQs that you may find useful. Also, if you are completely new to this, Sun's JDBC Tutorial is helpful too.
 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Paul!

I am not completely new to JDBC.
I have used the typical code to connect to databases and fetched result sets and traversed them etc.

I was doing all this without paying attention to why a certain way to connect to a db and why not some other way.

I was going through the JDBC faq also but did not find a satisfactory answer to my query.

So I am trying to make my concepts as clear as possible.

I will be going through the links given by you.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic