• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Class.forName()

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
}
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic