Hi,
Several times we saw the code like this:
Class.forName("com.mysql.jdbc.Driver");
Connection connection = null;
connection = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","uname", "pass");
When Class.forName(com.mysql.jdbc.Driver") executes
then it will search for the Driver class in package com.mysql.jdbc.
If it finds then it loads the Driver class and instantiate it,
otherwise it will throw the "ClassNotFoundException".
During instantiation it will execute the static block of that "Driver class" which will register it to the "DriverManager" as under:
static
{
try
{
DriverManager.registerDriver(new Driver());
}
catch(SQLException E)
{
throw new RuntimeException("Can't register driver!");
}
}
Eg"- If we try to make the connection to the SQLdatabase and we write the Class.forName("oracle.jdbc.OracleDriver"); then it does not make the connection.
For connection to the SQL DB we must have registered the SQL Driver.
This happens to every database like Oracle, SQL, Microsoft, hsql, derby etc.
And we can create db connections to different database as under:
Class.forName("oracle.jdbc.OracleDriver");
Class.forName("com.mysql.jdbc.Driver");
Connection sqlConnection = null;
Connection oracleConnection = null;
In the above code, DriverManager tries to make the connection by using all the drivers registered, if using the first driver the connections is not established, then it tries the next one until the connection is established or the drivers are finished.
Hope this information is helpful.
Regards
Rajan Bansal
Several times we saw the code like this:
Class.forName("com.mysql.jdbc.Driver");
Connection connection = null;
connection = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","uname", "pass");
When Class.forName(com.mysql.jdbc.Driver") executes
then it will search for the Driver class in package com.mysql.jdbc.
If it finds then it loads the Driver class and instantiate it,
otherwise it will throw the "ClassNotFoundException".
During instantiation it will execute the static block of that "Driver class" which will register it to the "DriverManager" as under:
static
{
try
{
DriverManager.registerDriver(new Driver());
}
catch(SQLException E)
{
throw new RuntimeException("Can't register driver!");
}
}
This registration is important because when "connection = DriverManager.getConnection(
"jdbc:mysql://hostname:port/dbname","username", "password");" executes then it tries to make the database connection using that Driver.Eg"- If we try to make the connection to the SQLdatabase and we write the Class.forName("oracle.jdbc.OracleDriver"); then it does not make the connection.
For connection to the SQL DB we must have registered the SQL Driver.
This happens to every database like Oracle, SQL, Microsoft, hsql, derby etc.
And we can create db connections to different database as under:
Class.forName("oracle.jdbc.OracleDriver");
Class.forName("com.mysql.jdbc.Driver");
Connection sqlConnection = null;
Connection oracleConnection = null;
sqlConnection = DriverManager.getConnection(
"jdbc:mysql://hostname:port/dbname","username", "password");"
oracleConnection = DriverManager.getConnection(
"jdbc:oracle://hostname:port/dbname","username", "password");"In the above code, DriverManager tries to make the connection by using all the drivers registered, if using the first driver the connections is not established, then it tries the next one until the connection is established or the drivers are finished.
Hope this information is helpful.
Regards
Rajan Bansal
Hi friends,
ReplyDeleteWe don't need Class.forName("driverClass"); anymore, if we want to connect to the database.
because in the JDBC API 4.0, the DriverManager.getConnection method is enhanced to load JDBC drivers automatically. Therefore, applications do not need to call the Class.forName method to register or load the driver when using the sqljdbc4.jar class library.
However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.
Note: JDBC 4.0 is included in the release of JAVA SE6.0 i.e. Dec 11,2006