What is the actual use of Class.forName( oracle.jdbc.driver.OracleDriver ) while connecting to a database?
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 typejava.sql.Driver
. Each JDBC driver contains one or more classes that implements the interfacejava.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 methodClass.forName
.)
Further reading (read: questions this is a dup of)
- What purpose does Class.forName() serve if you don't use the return value?
- How does Class.forName() work?
- What does 'Class.forName("org.sqlite.JDBC");' do?
- What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
- Loading JDBC driver
Class.forName( oracle.jdbc.driver.OracleDriver ); throwing NullPointerException
Check that the Oracle JDBC driver is in your classpath.
You can find one on the page below if not yet downloaded:
http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html
oracle.jdbc.driver.OracleDriver ClassNotFoundException
Class.forName("oracle.jdbc.driver.OracleDriver");
This line causes ClassNotFoundException
, because you haven't placed ojdbc14.jar
file in your lib folder of the project. or YOu haven't set the classpath
of the required jar
ojdbc14 external jar file raises not found exception
This might help ojdbc14.jar vs. ojdbc6.jar
Are you sure that's the correct definition in the POM?
I use this:
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>18.3.0.0</version>
</dependency>
Check for the specific version of Oracle and the proper jar version. It should not need the system path.
What is the purpose of 'Class.forName( MY_JDBC_DRIVER )'?
First of: with modern JDBC drivers and a current JDK (at least Java 6) the call to Class.forName()
is no longer necessary. JDBC driver classes are now located using the service provider mechanism. You should be able to simply remove that call and leave the rest of the code unchanged and it should continue to work.
If you're not using a current JDK (or if you have a JDBC driver that does not have the appropriate files set up to use that mechanism) then the driver needs to be registered with the DriverManager
using registerDriver
. That method is usually called from the static initializer block of the actual driver class, which gets triggered when the class is first loaded, so issuing the Class.forName()
ensures that the driver registers itself (if it wasn't already done).
And no matter if you use Class.forName()
or the new service provider mechanism, you will always need the JDBC driver on the classpath (or available via some ClassLoader
at runtime, at least).
tl;dr: yes, the only use of that Class.forName()
call is to ensure the driver is registered. If you use a current JDK and current JDBC drivers, then this call should no longer be necesary.
ORA-01882: timezone region not found
You may also try to check the version of the Oracle jdbc driver and Oracle database. Just today I had this issue when using ojdbc6.jar (version 11.2.0.3.0) to connect to an Oracle 9.2.0.4.0 server. Replacing it with ojdbc6.jar version 11.1.0.7.0 solved the issue.
I also managed to make ojdbc6.jar version 11.2.0.3.0 connect without error, by adding oracle.jdbc.timezoneAsRegion=false
in file oracle/jdbc/defaultConnectionProperties.properties (inside the jar). Found this solution here (broken link)
Then, one can add -Doracle.jdbc.timezoneAsRegion=false
to the command line, or AddVMOption -Doracle.jdbc.timezoneAsRegion=false
in config files that use this notation.
You can also do this programmatically, e.g. with System.setProperty
.
In some cases you can add the environment variable on a per-connection basis if that's allowed (SQL Developer allows this in the "Advanced" connection properties; I verified it to work when connecting to a database that doesn't have the problem and using a database link to a database which has).
Related Topics
The Performance Impact of Using Instanceof in Java
Controlling Maven Final Name of Jar Artifact
How to Put Axis on a .Png File in Java
Convert Java.Util.Date to What "Java.Time" Type
Java Filewriter with Append Mode
How to Split a Comma-Separated String
Difference Between Paint, Paintcomponent and Paintcomponents in Swing
Why Would a Static Nested Interface Be Used in Java
Eclipse: How to Build an Executable Jar with External Jar
Java Unsupported Major Minor Version 52.0
Abstracttablemodel Gui Display Issue
Java.Lang.Unsatisfiedlinkerror No *****.Dll in Java.Library.Path
Parsing Iso-8601 Datetime with Offset with Colon in Java
Java String Remove All Non Numeric Characters But Keep the Decimal Separator
Java - Class.Getresource Returns Null
Polymorphism: Why Use "List List = New Arraylist" Instead of "Arraylist List = New Arraylist"