About Question enthuware.ocpjp.v8.2.1375 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
rvt1234
Posts: 10
Joined: Mon Aug 18, 2014 5:25 am
Contact:

About Question enthuware.ocpjp.v8.2.1375 :

Post 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?

admin
Site Admin
Posts: 9358
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

rvt1234
Posts: 10
Joined: Mon Aug 18, 2014 5:25 am
Contact:

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

Post by rvt1234 »

Paul,

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

Robbie
Posts: 13
Joined: Sun Mar 27, 2016 1:10 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 9358
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, unless explicitly mentioned you should assume JDBC 4.0.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 9358
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 9358
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests