1

I've used once a GSON library in Eclipse and it was very easy. Just added it as external library and in source file I've imported it and was able to use it just like any other class in Java. Example:

Gson gson = new Gson(); //and just use it

Recently I had to work with SQLIte database file, so I've downloaded JDBC driver library and added it in my Eclipse project. But I've noticed that there is kind of strange (at least for me) syntax for using it. I've imported java.sql.* but to be able to use its classes I had to do the following:

 Class.forName("org.sqlite.JDBC");

I know that the return value from this command is Class object (during runtime) but as you can see from the syntax it is never used.

Please explain whats going on there and why I can't just use SQL package classes without invoking Class.forName first.

dev2d
  • 4,245
  • 3
  • 31
  • 54
JobNick
  • 473
  • 1
  • 6
  • 18
  • possible duplicate of [What does 'Class.forName("org.sqlite.JDBC");' do?](http://stackoverflow.com/questions/6740601/what-does-class-fornameorg-sqlite-jdbc-do) – Mark Rotteveel May 02 '14 at 10:33

2 Answers2

1

It loads a class dynamically. What does Class.forname method do? is a good article about it and it also explains why database drivers needs it:

Let's see why you need Class.forName() to load a driver into memory. All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has static an initializer only.

The MySQL JDBC Driver has a static initializer looks like this:

static {
    try {
        java.sql.DriverManager.registerDriver(new Driver());
    } catch (SQLException E) {
        throw new RuntimeException("Can't register driver!");
    }
}

JVM executes the static block and the Driver registers itself with the DriverManager.

You need a database connection to manipulate the database. In order to create the connection to the database, the DriverManager class has to know which database driver you want to use. It does that by iterating over the array (internally a Vector) of drivers that have registered with it and calls the acceptsURL(url) method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL.

Source - What does 'Class.forName("org.sqlite.JDBC");' do?

Community
  • 1
  • 1
jamierocks
  • 685
  • 5
  • 16
  • I think it will be a cleaner way to force driver to register itself is using static method lets say Init. the Class.forName looks kinda like a work around to cause JVM invoke driver's static initializer. – JobNick May 01 '14 at 22:07
  • Instead of copying an answer you could have flagged as duplicate to that question. – Mark Rotteveel May 02 '14 at 10:35
  • @MarkRotteveel Oh, sorry I knew it was possible to flag as duplicate but thought it was only for admins. I was only trying to help, as I a new to StackOverFlow myself :) – jamierocks May 02 '14 at 14:51
  • @JobNick with a modern JDBC driver (that is a driver which has a JDBC 4 compliant `META-INF/services/java.sql.Driver` file), the driver will be loaded automatically. Also originally one of the reasons for using the `Class.forName` was to simplify loading in component based application (eg a database tool that loads the driver class specified by a user, or an application orchestrated from multiple components). Having an `init()` method would require loading the class and then invoking the init method by reflection; actions which are not guaranteed thread-safe (where as class-loading is) – Mark Rotteveel May 02 '14 at 14:56
  • @malgm No problem. Technically everyone is a moderator in stackoverflow, but your powers depend on your current reputation. Flagging will notify other people with more powers, and eventually you are allowed to cast a close vote yourself, etc. – Mark Rotteveel May 02 '14 at 14:57
0

Basically it lets the JVM register which implementation of the java.sql.* libraries your code is using. This way you can rely on a standard interface without going through the hoops and hurdles that require implementation-level details.

Pedantic
  • 5,032
  • 2
  • 24
  • 37