Page 1 of 1

About Question enthuware.ocpjp.v8.2.1375 :

Posted: Mon Apr 11, 2016 5:39 am
by rvt1234
When Connection c = DriverManager.getConnection("jdbc:fandu://localhost:1234/myDB", "user", "pwd"); doesn't load the driver,
at what point in time is the driver actually loaded?

Re: About Question enthuware.ocpjp.v8.2.1375 :

Posted: Mon Apr 11, 2016 10:55 am
by admin
As per http://docs.oracle.com/javase/tutorial/ ... cting.html:
When this class first attempts to establish a connection, it automatically loads any JDBC 4.0 drivers found within the class path.
Further on the same page it also says,
Any JDBC 4.0 drivers that are found in your class path are automatically loaded.
As per https://docs.oracle.com/javase/7/docs/a ... nager.html,
As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property. This allows a user to customize the JDBC Drivers used by their applications. For example in your ~/.hotjava/properties file you might specify:

jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:

my.sql.Driver

Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.
So basically DriverManager.getConnection will probably cause the driver to load. But it is not a "required" thing for loading a driver. The driver could be loaded even before anyone calls getConnection.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v8.2.1375 :

Posted: Mon Apr 11, 2016 1:19 pm
by rvt1234
Paul,

the devil is in the details! Thank's for explaining this clearly.

Re: About Question enthuware.ocpjp.v8.2.1375 :

Posted: Sat Apr 16, 2016 3:00 am
by Robbie
Shouldn't the question mention that it is about a JDBC 4.0 driver? Or can I assume all question on the exam are about 4.0?

Re: About Question enthuware.ocpjp.v8.2.1375 :

Posted: Sat Apr 16, 2016 4:19 am
by admin
Yes, unless explicitly mentioned you should assume JDBC 4.0.

Re: About Question enthuware.ocpjp.v8.2.1375 :

Posted: Sat Jul 09, 2016 10:01 pm
by johnlong
Hi

Here it says
DriverManager: This fully implemented class connects an application to a data source, which is specified by a database URL. When this class first attempts to establish a connection, it automatically loads any JDBC 4.0 drivers found within the class path. Note that your application must manually load any JDBC drivers prior to version 4.0.
http://docs.oracle.com/javase/tutorial/ ... cting.html

Does it mean that you have to call DriverManager.getConnection to load the drivers, thus answer 1 shall be correct one?
Any JDBC 4.0 drivers that are found in your class path are automatically loaded.
Yes, but after you call DriverManager.getConnection only.
Please check on referred page.

Re: About Question enthuware.ocpjp.v8.2.1375 :

Posted: Sat Jul 09, 2016 10:17 pm
by admin
Yes, DriverManager does load the drivers. But that may not necessarily be the only way to load the drivers. Nowhere does it say that only DriverManager can load the drivers.

Re: About Question enthuware.ocpjp.v8.2.1375 :

Posted: Tue Apr 06, 2021 3:46 pm
by jme_chg
so if the question was

Which of the following lines of code can get the driver loaded?

instead of

Which of the following lines of code is/are required to get the driver loaded?

then A would have been correct?

Re: About Question enthuware.ocpjp.v8.2.1375 :

Posted: Tue Apr 06, 2021 8:41 pm
by admin
Yes, correct.