MIDRANGE dot COM Mailing List Archive



Home » JAVA400-L » November 2000

Re: JAVA/JDBC newbie question


  • Subject: Re: JAVA/JDBC newbie question
  • From: Todd Chaffee <tc@xxxxxxxxxxx>
  • Date: Tue, 21 Nov 2000 11:49:05 -0500

fixed

Tom,

This is kind of a deep subject for a "newbie" because it concerns the JVM, 
class loading, and drivers.  Here's my attempt with the warning that it's 
not really that important to know what "Class.forname()" actually 
does.  For a beginner, it's good enough to just to know that it's required 
before using JDBC.

Anyway, "Class.forname()" loads the class.  All objects that you create in 
a java program actually have to have their class loaded first.  Most of the 
time this is done automatically for you by the java virtual machine (JVM) 
and you never know about it.

I'm not sure why, but some (all?) drivers require that you load the class 
explicitly.  Note that for most non-driver classes you would also have to 
create instances (actual objects) of the class before you could use them, 
however, most driver authors include static initializer code in the driver 
class that will instantiate (i.e. create an object) the driver and register 
it with the DriverManager class.  In other words, you just load the class 
using "Class.forname()" and everything else is taken care of for 
you.  There are exceptions to this (depending on the JVM) in which the 
static initializier code does not get executed and you must manually 
instantiate and register the driver but these are becoming rarer.

There is another rarely talked about way to "load a class" by using the 
command line like so:

java Djdbc.drivers=COM.ibm.db2.jdbc.app.DB2Driver TestApp

The advantage to this is that your code is not tied to the specific driver 
you use.

Remember that no matter how you do it, the driver class must be in your 
class path or you will get an exception.

The October 2000 issue of Java Developers Journal has a good article on 
JDBC  in the "Java Basics" column by Robert J. Brunner.  It gives a more 
detailed description of how "Class.forname()" is used specific to 
JDBC.  You can view the issue online in the archives area .  Click on the 
"Digital Edition" button and follow the links until you get to the "Access 
Archives" link.  You'll need to register, but it's free.

http://www.sys-con.com/java/index2.html


To find out more about the "Class" class you can look at Sun's 
documentation although I found it kind of confusing:

http://java.sun.com/j2se/1.3/docs/api/java/lang/Class.html

For some real brain-twisting deep technical stuff about loading classes, 
see the "Java Virtual Machine Specification" and the "Java Language 
Specification".

http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html

http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html

I have no affiliation with any of the sites or companies mentioned above 
except that I use them :)

- Todd Chaffee
Arkay Computer Consultants
http://www.arkayusa.com


At 09:47 AM 11/21/00 -0500, you wrote:



>I have seen the following code and would like to know what it does:
>
>Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
>
>The documentation says it "enables Java to find the driver classes for
>handling JDBC objects."
>
>Specifically, what does the "Class.forName"  do?
>
>Thanks for your help.
>
>tom t
>
>
>+---
>| This is the JAVA/400 Mailing List!
>| To submit a new message, send your mail to JAVA400-L@midrange.com.
>| To subscribe to this list send email to JAVA400-L-SUB@midrange.com.
>| To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com.
>| Questions should be directed to the list owner: joe@zappie.net
>+---

+---
| This is the JAVA/400 Mailing List!
| To submit a new message, send your mail to JAVA400-L@midrange.com.
| To subscribe to this list send email to JAVA400-L-SUB@midrange.com.
| To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner: joe@zappie.net
+---





Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2014 by MIDRANGE dot COM and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available here. If you have questions about this, please contact