0

What is a Driver Manager and why is it required when you use JDBC to connect to a database? I tried connecting to the database without the given below statement:

Class.forName("com.mysql.jdbc.Driver").newInstance();

But it fails. So, what does this line of code do when connecting to mysql database using java?

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
Sonia Saxena
  • 95
  • 3
  • 11
  • With recent driver versions and Java 6 / JDBC 4 or higher that line is entirely irrelevant. – Mark Rotteveel Apr 06 '15 at 08:34
  • possible duplicate of [What is the purpose 'Class.forName("MY\_JDBC\_DRIVER")'?](http://stackoverflow.com/questions/7662902/what-is-the-purpose-class-fornamemy-jdbc-driver) – Mark Rotteveel Apr 06 '15 at 08:39

2 Answers2

1

If you check the Oracle docs you will get the clear picture.

The basic service for managing a set of JDBC drivers.

Also from Oracle Docs

First, you need to establish a connection with the data source you want to use. A data source can be a DBMS, a legacy file system, or some other source of data with a corresponding JDBC driver. Typically, a JDBC application connects to a target data source using one of two classes:

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.

DataSource: This interface is preferred over DriverManager because it allows details about the underlying data source to be transparent to your application. A DataSource object's properties are set so that it represents a particular data source. See Connecting with DataSource Objects for more information. For more information about developing applications with the DataSource class, see the latest The Java EE Tutorial.

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

This line of code returns a new object of driver class loaded into memory.

DriverManager class aids to connect to data source. Register and De-register driver class. Set login time out counter.

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
XTR-Cold
  • 39
  • 3