java - Why need for using JDBC write Class.forName(...) for each connection? -


if want connect database should write code this:

class.forname("oracle.jdbc.driver.oracledriver"); connection con = drivermanager.getconnection("jdbc:oracle:thin:@localhost:1521/mysidorservicename", "sysdba", "password123"); 

why should load concrete driver before connect database? result of class.forname statement ignored - loaded class not associated drivermanager. can load drivers used different databases in moment of start application , not write class.forname code before each connection?

older drivermanager implementations (before jdbc 4.0, park of jdk 6), required drivers have static block register them drivermanager. static blocks called once when class loaded driver manager. question - doesn't matter load these classes, long load drivers before attempting use them.

since jdbc 4.0 (which is, mentioned above, part of jdk 6), don't have call class.forname @ all, though. quote drivermanager's javadoc:

the drivermanager methods getconnection , getdrivers have been enhanced support java standard edition service provider mechanism. jdbc 4.0 drivers must include file meta-inf/services/java.sql.driver. file contains name of jdbc drivers implementation of java.sql.driver. example, load my.sql.driver class, meta-inf/services/java.sql.driver file contain entry [etc..]

in layman's words, it's driver register declare provides jdbc service given connection string, , can remove class.forname calls code.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -