wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Class.forName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class.forName()" Watch "Class.forName()" New topic
Author

Class.forName()

Glenny Dsilva
Ranch Hand

Joined: May 09, 2005
Posts: 42
I am not clear with what does Class.forName().

I read that Class.forName is used to dynamically load a class.
For e.g. the jdbc driver that we use is loaded when u

write

Class.forName("com.mysql.jdbc.Driver");

but one can also do it with extracting the jar file and importing the package


Using the code below i have connected to the database so what is the benefit of using
Class.forName().


package foo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import com.mysql.jdbc.Driver;

public class l
{
public static void main(String args[])
{
try
{
Driver.class.newInstance();

Connection con = DriverManager.getConnection("jdbc:mysql://localhost/effectiveinfo","root","rkn09876");
Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("Select * From EmpMst Limit 10");

while(rs.next())
{
System.out.println(rs.getString(1));
}
rs.close();
System.out.println(con.getCatalog());
}catch(Exception e)
{
System.out.println("Exception" + e.toString());
}
}
}
Martin Wingert
Greenhorn

Joined: Oct 10, 2005
Posts: 16
You write Class.forName("drivername") because you want to load the drivers class so that it will register as a driver to the DriverManager. You have just altered the syntax a bit by importing the driver class and you use Driver.class.newInstance() to load the driver into memory so that it will be possible to use the DriverManager to create the connection.

In most tutorials on jdbc the Class.forName() method is used so that seems to be the most common way to load the driver. So if you're looking for a benefit of using the Class.forName() that's it as far as I can see. And writing code according to the most common practise isn't a bad reason for chosing the Class.forName way.

There is also the possibility to load the driver as a system propery when you execute your program. The property is called jdbc.drivers.

Hope that I managed to explain anything :-)
Martin Wingert
Greenhorn

Joined: Oct 10, 2005
Posts: 16
Another drawback with your solution is that it will be harder to change database source later since you have hardcoded the drivers path into your sourcecode. If you pass the drivername to your application by the command line or a configuration file and use the Class.forName() way then you don't need to recompile your code when changing database.

And a last little thing. This type of question seems to be more suited under the JDBC thread since connecting to a database isn't the first thing you do when making a java program. At least if you're new to programming.

But that's just a greenhorns opinion
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Yes, Class.forName lets you dynamically load a class.
This is a powerful thing, and its usefulness goes
way beyond the JDBC -- that's just the place you saw
it used for the first time.

Typical example: This is an interface, say I, and you
want to configure an app so that the class used to implement
this class can be changed without recompiling the source code:

If you want to call other constructors than the no-arg one,
that can be done using class's getConstructor method and
java.lang.reflect.Constructor.

If you're a Java beginner you probably have little use for
this technique, but once you get into frameworks like Struts
or Java ServerFaces, you'll see this being heavily used.


There is no emoticon for what I am feeling!
 
Don't get me started about those stupid light bulbs.
 
subject: Class.forName()