-2

I have done a basic copy-paste of code to connect to an oracle server here at work:

import java.sql.Connection;
import java.sql.DriverManager;

public class OracleClass {
    protected String driverName = "oracle.jdbc.driver.OracleDriver";
    protected Connection dbConnection;
    protected String username = "someUsername";
    protected String password = "somePassword";
    protected String server = "MYSERVER";
    protected String port = "1521";
    protected String sid = "MYSID";

    public OracleClass() {
        String url = "jdbc:oracle:thin:@" + server + ":" + port + ":" + sid;
        Class.forName(driverName);   // What does this do???
        dbConn = DriverManager.getConnection(url, username, password);
    }

    /** 
     * Other methods
     */
}

This works, connects, and I can query the database and get a ResultSet from it, but what does the Class.forName(DriverName) line do? It is supposed to return a static class, but I am no assigning it to anything (which I would think I need to do). It looks like it is just doing nothing.

What does it do? Is it needed?

Community
  • 1
  • 1
Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28
  • 3
    With any modern driver `Class.forName(DriverName)` is no longer necessary –  Oct 23 '15 at 13:10
  • **Please** search before posting. A simple "what does Class.forName do site:stackoverflow.com" gives you several answers to **exactly** this question. – T.J. Crowder Oct 23 '15 at 13:11
  • If this is a duplicate then where is the question that asks and is answered that this is a line which is no longer necessary, please? I did not find this and I searched (see link) and did not find an answer as to why this is needed nor as to what it does. – Rabbit Guy Oct 23 '15 at 13:13
  • @blahfunk: Again: Do the search. Look through the first, oh, 10 or so answers on SO. Someone will have flagged up that modern drivers don't need it (I'm trusting a_horse_with_no_name and a couple of others I've seen around saying that, back when I dealt with JDBC, they still did). – T.J. Crowder Oct 23 '15 at 13:22
  • @T.J.Crowder is right. Seems like 1 out of every 5 questions on SO JDBC tag is about `Class.forName()` or confusion with why driver classes are not getting loaded. – Andy Guibert Oct 23 '15 at 13:32

1 Answers1

2

From the documentation:

Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to: Class.forName(className, true, currentLoader) where currentLoader denotes the defining class loader of the current class.

This is essentially an old idiom in Java SQL boilerplate code to ensure the driver class can be loaded and is loaded, prior to using the driver.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • Actually this was used because there were static blocks in the JDBC classes that got executed and filled various properties so one could use this driver. – Aron_dc Oct 23 '15 at 13:12
  • 1
    @Aron_dc yes that's the gist of it. Upon class loading static initializers are executed, which needed to be ensured prior to using the driver. – Mena Oct 23 '15 at 13:13