java - Why is JDBC driver not necessary for H2 -


i have simple java getting started app using h2 in-memory db:

string db_url = "jdbc:h2:./test"; try(connection conn = drivermanager.getconnection(db_url,user,pass);                 statement stmnt = conn.createstatement() ){     // executing sqls, getting result set, etc.     ... } // catching & handling exceptions 

the app works, however, wonder why don't have call class.forname(jdbc_driver); anywhere in code? in h2 quick starts, load class manually.

you don't need jdbc driver not many years old. drivers auto-discovered service provider mechanism of java, as documented:

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:

my.sql.driver 

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

note that, contrary title of question says, jdbc driver necessary. using class.forname() load unnecessary.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -