java - What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'? -


i understand class loading useful load class @ runtime class name.

however while using jdbc in our project know driver going use , driver manager string hard coded.

my question is: why loading driver using class.forname("jdbc_driver") here?
why can't go ahead adding driver in class path? since know driver jar going use.

i believe class.forname(jdbc_driver) load driver drivermanager. reason?

edit 1:

the drivermanager api doc states that

as part of its(drivermanager) initialization, drivermanager class attempt load driver classes referenced in "jdbc.drivers" system property.

applications no longer need explictly load jdbc drivers using class.forname(). existing programs load jdbc drivers using class.forname() continue work without modification.

then when use other oracle driver; need change driver name string in system property?

first of: modern jdbc drivers , current jdk (at least java 6) call class.forname() no longer necessary. jdbc driver classes located using service provider mechanism. should able remove call , leave rest of code unchanged , should continue work.

if you're not using current jdk (or if have jdbc driver not have appropriate files set use mechanism) driver needs registered drivermanager using registerdriver. method usually called static initializer block of actual driver class, gets triggered when class first loaded, issuing class.forname() ensures driver registers (if wasn't done).

and no matter if use class.forname() or new service provider mechanism, always need jdbc driver on classpath (or available via classloader @ runtime, @ least).

tl;dr: yes, only use of class.forname() call ensure driver registered. if use current jdk , current jdbc drivers, call should no longer necesary.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -