mysql - the diference Java Connection method (ClassForname & RegisterDriver) -
i know 2 ways connect database divided 2 methods (method1 , method 2)
connection connection; public void getconnectionmethod1() { try { class.forname("net.sourceforge.jtds.jdbc.driver"); connection = drivermanager.getconnection("jdbc:jtds:sqlserver://localhost:1433/latihan","sa","denni"); } catch (sqlexception e) { } } public void getconnectionmethod2() { try { drivermanager.registerdriver(new net.sourceforge.jtds.jdbc.driver()); connection = drivermanager.getconnection("jdbc:jtds:sqlserver://localhost:1433/latihan","sa","denni"); } catch (sqlexception e) { } }
my question is; there difference between them ? method 1 uses class.forname
while method 2 uses registerdriver
what advantages , disadvantages between them?
note : can use preparedstatement on method 2.
from jdbc 4.0,java developers no longer need explicitly load jdbc drivers using code class.forname() register jdbc driver. drivermanager class takes care of automatically locating suitable driver when drivermanager.getconnection() method called. feature backward-compatible, no changes needed existing jdbc code.
Comments
Post a Comment