• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 20228
  • Last Modified:

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?
0
william007
Asked:
william007
  • 3
  • 2
  • 2
  • +5
5 Solutions
 
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
 
objectsCommented:
not sure, Sun *may* have it available. its probably mostluy native code.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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
 
william007Author Commented:
Thanks:-)
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
no worries :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now