File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Difference Between Class.forName & import statement in JDBC Applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Difference Between Class.forName & import statement in JDBC Applications" Watch "Difference Between Class.forName & import statement in JDBC Applications" New topic
Author

Difference Between Class.forName & import statement in JDBC Applications

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Hi This is a very common scenerio that we use in any jdbc application ...

Class.forName(//some jdbc.odbc class name should be declared here)

now when we read the documentation of forName method it's saying that it is used to load the particular class. And the same thing we can do by import statement in the starting of the program.
Now this is leading a confusion that why we use Class.forName method when we can import that Package and if we cann't import those classes then what is the exactly usage of Class.forName functionalty. Please reply asap with appropriate functional difference.


The Best way to predict your future is to create it - Every great individual common man
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If you import the class, you would need to change the import and recompile the class to use a different driver. JDBC is designed to create a lose coupling between the driver and JDBC code, and you lose many of the benefits of JDBC by hard-coding the driver. Admittedly you may not always all these features from JDBC, but why rule them out if you may need them later?
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Thankx David ,

But I am still confused could you please eloborate it more in some easy technical words. or if possible then with some example
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Consider these two pieces of code, which do the same thing:





In both cases the class would have to be changed if the driver changed, but consider this variation on the first example:



Now we haven't defined how this code gets the driver name from the preoprties, but you can do it any of a number of ways. The point is that the driver type is now a matter of configuration, and can be changed without requiring a recompilation of the code. Whenever you aim to distribute code or support multiple databases easily you need to do this.

There is a little bit more to it, such as using standard SQL and not using the additional methods on the DRiver classes, but this should give you an idea of what I mean.

Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference Between Class.forName & import statement in JDBC Applications