What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database?

JavaJdbc

Java Problem Overview


What will the command

Class.forName("oracle.jdbc.driver.OracleDriver")

exactly do while connecting to a Oracle database? Is there an alternate way of doing the same thing?

Java Solutions


Solution 1 - Java

It obtains a reference to the class object with the FQCN (fully qualified class name) oracle.jdbc.driver.OracleDriver.

It doesn't "do" anything in terms of connecting to a database, aside from ensure that the specified class is loaded by the current classloader. There is no fundamental difference between writing

Class<?> driverClass = Class.forName("oracle.jdbc.driver.OracleDriver");
// and
Class<?> stringClass = Class.forName("java.lang.String");

Class.forName("com.example.some.jdbc.driver") calls show up in legacy code that uses JDBC because that is the legacy way of loading a JDBC driver.

From The Java Tutorial:

> In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. This methods required an object of type java.sql.Driver. Each JDBC driver contains one or more classes that implements the interface java.sql.Driver.
> ...
> Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

Further reading (read: questions this is a dup of)

Solution 2 - Java

It registers the driver; something of the form:

public class SomeDriver implements Driver {
  static {
    try {
      DriverManager.registerDriver(new SomeDriver());
    } catch (SQLException e) {
      // TODO Auto-generated catch block
    }
  }

  //etc: implemented methods
}

Solution 3 - Java

From the Java JDBC tutorial:

> In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. > Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

So, if you're using the Oracle 11g (11.1) driver with Java 1.6, you don't need to call Class.forName. Otherwise, you need to call it to initialise the driver.

Solution 4 - Java

Pre Java 6 the DriverManager class wouldn't have known which JDBC driver you wanted to use. Class.forName("...") was a way on pre-loading the driver classes.

If you are using Java 6 you no longer need to do this.

Solution 5 - Java

This command loads class of Oracle jdbc driver to be available for DriverManager instance. After the class is loaded system can connect to Oracle using it. As an alternative you can use registerDriver method of DriverManager and pass it with instance of JDBC driver you need.

Solution 6 - Java

An alternative would to use the jdbc.drivers System property to specify your required drivers(s) on the command line when you start the JVM.

Solution 7 - Java

Use oracle.jdbc.OracleDriver, not oracle.jdbc.driver.OracleDriver. You do not need to register it if the driver jar file is in the "WEB-INF\lib" directory, if you are using Tomcat. Save this as test.jsp and put it in your web directory, and redeploy your web app folder in Tomcat manager:

<%@ page import="java.sql.*" %>

<HTML>
<HEAD>
<TITLE>Simple JSP Oracle Test</TITLE>
</HEAD><BODY>
<%
Connection conn = null;
try {
	Class.forName("oracle.jdbc.OracleDriver");
	conn = DriverManager.getConnection("jdbc:oracle:thin:@XXX.XXX.XXX.XXX:XXXX:dbName", "user", "password");
	Statement stmt = conn.createStatement();
	out.println("Connection established!");
}
catch (Exception ex)
{
	out.println("Exception: " + ex.getMessage() + "");

}
finally
{
	if (conn != null) {
		try {
			conn.close();	
		}
		catch (Exception ignored) {
			// ignore
		}
	}
}

%>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAravindView Question on Stackoverflow
Solution 1 - JavaMatt BallView Answer on Stackoverflow
Solution 2 - JavaMcDowellView Answer on Stackoverflow
Solution 3 - JavaJonathanView Answer on Stackoverflow
Solution 4 - JavaQwerkyView Answer on Stackoverflow
Solution 5 - JavaanatolichView Answer on Stackoverflow
Solution 6 - JavasudocodeView Answer on Stackoverflow
Solution 7 - JavaTomView Answer on Stackoverflow