Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

When connecting to using Type 1 JDBC drivers, we do something like below
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:AccessConn", "", "");

But what is
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
done?

Base on my understanding
Class c=Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
we can assign it for the variable c, so we can use variable c for other operation,
but simply
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); without assign it to any variable, what will be done behind the scene?
LVL 9
william007Asked:
Who is Participating?
 
Mayank SAssociate Director - Product EngineeringCommented:
Basically, every driver would register itself with the DriverManager by calling the registerDriver () method in a static block, so all you need to do is load the class (hence the static block will be executed and the driver will be registered - you need not hold a reference to the Class object). You can read more about class-loading:

http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html

http://java.sun.com/docs/books/tutorial/ext/basics/load.html
0
 
objectsCommented:
It is to load the driver classthe drive class will then register itself with the DriverManager so it knows about it when you call getConnection()
0
 
william007Author Commented:
May I know where can I access the source code of sun.jdbc.odbc.JdbcOdbcDriver? so I can take a look of it..
I do not found it under the source folder
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
objectsCommented:
not sure, Sun *may* have it available. its probably mostluy native code.
0
 
CEHJCommented:
The following gives you an idea of how this works with DriverManager:

http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/drivermanager.html
0
 
Vinod_VijayanVinCommented:
ODBC - Open Database Connectivity
JDBC  - Java Database Connectivity

The ODBC specification offers a procedural API for using SQL queries to access data.
An implementation of ODBC will contain one or more applications, a core ODBC library, and one or more "database drivers".
The core library, independent of the applications and DBMS systems, acts as an "interpreter" between the applications and the database drivers, whereas the database drivers contain the DBMS-specific details.
 Thus a programmer can write applications that use standard types and features without concern for the specifics of each DBMS that the applications may encounter. Likewise, database driver implementors need only know how to attach to the core library.
The core ODBC library is written in C language.
So the JDBC driver needs a bridge for communication with ODBC that is provided by the JDBC-ODBC bridge driver.
0
 
hoomanvCommented:
> May I know where can I access the source code of sun.jdbc.odbc.JdbcOdbcDriver?

in your j2se install directory open jre1.5.0_01\lib\rt.jar file with an archive manager
then extract \sun\jdbc\odbc\JdbcOdbcDriver.class from the jar file
decompile it and you have the source code.

here is the static constructor of JdbcOdbcDriver which will be loaded by calling Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

      static  {
            JdbcOdbcTracer jdbcodbctracer = new JdbcOdbcTracer();
            if (jdbcodbctracer.isTracing())
                  jdbcodbctracer.trace("JdbcOdbcDriver class loaded");
            JdbcOdbcDriver jdbcodbcdriver = new JdbcOdbcDriver();
            try {
                  DriverManager.registerDriver(jdbcodbcdriver);
            }
            catch (SQLException sqlexception) {
                  if (jdbcodbctracer.isTracing())
                        jdbcodbctracer.trace("Unable to register driver");
            }
      }
0
 
borislavmarkovCommented:
Statis initializations are made, driver registrations, etc.
0
 
WebstormCommented:
>> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
load the class in the JVM class pool.
So you don't need to hold it in a Class variable if you don't use it.
0
 
william007Author Commented:
Thanks:-)
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
no worries :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.