DriverManager class
It's used for establishing JDBC connections and for managing JDBC drivers. JDBC 2.0 API has a new interface DataSource and the preferred means of connecting to a data source has now become by using a DataSource object.
DriverManager tries to load the driver classes mentioned in the 'jdbc.drivers' system property while the initialization. You can explicitly load JDBC drivers at any time by using the 'Class.forName("driver class");' syntax.
getConnection method of the DriverManager class tries to locate a suitable driver from amongst those loaded either during the initialization or by using Class.forName() explicitly. On successful invocation, this method returns a Connection object. You need to specify the database url having the subprotocol & subname, user id, password, etc. If any error occours during the database access then a SQLException is thrown.
Driver interface
Every databse driver needs to implement this interface. As mentioned above you can load as many databse drivers you want, but all of them must implement this interface. Each driver will provide a class implementing the Driver interface. Once these classes are loaded, an instance of it created and registered with the DriverManager. Loading, instance creation, and registration all done by calling the forName method 'Class.forName("driver class");'.
This method has a method named 'connect()', which accepts URL of the database and other required info of type java.util.Properties which contains the user id, password, etc. kind of information. This method returns either a Connection object on successful invocation. The failure can occur in two cases:
- If driver manager tries the url with the wrong driver (as it passes the url to every driver unless it either gets success or SQLException) in which case the method returns 'null'.
- Else if the driver manager tries the url with the right driver, but the connection doesn't get established due to some databse problems then the method returns SQLException.
2 comments:
Hi,
Was looking for details on drivermanager and I knew wud find it in ur site and did found. Keep up the good work.
--R
Thanks for the appreciation R. I hope, you'll keep finding articles of your interest here.
Keep Visiting/Posting!
Post a Comment