aspose file tools*
The moose likes JDBC and the fly likes driver loading differences Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "driver loading differences" Watch "driver loading differences" New topic
Author

driver loading differences

Johnson David
Greenhorn

Joined: May 22, 2005
Posts: 27
Hi friends ,

The fallowing are the different ways to load a driver

Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

why there are two ways,

what' s the difference ?

thanx

Johnson

[changed subject to something more self explanatory - was "help needed plz "]
[ January 26, 2006: Message edited by: Jeanne Boyarsky ]
Evgeniy Bulanov
Greenhorn

Joined: Dec 19, 2005
Posts: 23
2.3.2 Registering a JDBC Driver
You must register the Oracle driver, oracle.jdbc.driver.OracleDriver, in your program
before you use it. At this point, you may be confused because we've been talking about the OCI
and Thin drivers, but now we refer only to one class when registering. That's because the same
class file implements both drivers.
Registering the driver is the process by which the Oracle driver's class file is loaded into memory
so it can be utilized as an implementation of the JDBC interfaces. You need to do this only once
in your program. You can register a driver in one of three ways. The most common approach is to
use Java's Class.forName( ) method to dynamically load the driver's class file into memory,
which automatically registers it. This method is preferable because it allows you to make the
driver registration configurable and portable. The following example uses Class.forName( ) to
register the Oracle driver:
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException e) {
System.out.println("Oops! Can't find class
oracle.jdbc.driver.OracleDriver");
System.exit(1);
}
The second approach you can use to register a driver is to use the static
DriverManager.registerDriver( ) method. Use the registerDriver( ) method if you
are using a non-JDK compliant JVM, such as the one provided by Microsoft. For example:
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver( ));
}
catch(SQLException e) {
System.out.println("Oops! Got a SQL error: " + e.getMessage( ));
System.exit(1);
}
The third approach is to use a combination of Class.forName( ) to dynamically load the
Oracle driver and then the driver classes' getInstance( ) method to work around
noncompliant JVMs, but then you'll have to code for two extra Exceptions. To call the
getInstance( ) method for the dynamically loaded class, you can code the call as
Class.forName().newInstance( ):
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance( );
}
catch(ClassNotFoundException e) {
System.out.println("Oops! Can't find class
oracle.jdbc.driver.OracleDriver");
System.exit(1);
}
catch(IllegalAccessException e) {
System.out.println("Uh Oh! You can't load
oracle.jdbc.driver.OracleDriver");
System.exit(2);
}
catch(InstantiationException e) {
System.out.println("Geez! Can't instantiate
oracle.jdbc.driver.OracleDriver");
System.exit(3);
}

(Java Programming with Oracle JDBC ISBN: 0-596-00088-x, 496 pages)


Computers are like air conditioners - They stop working properly when you open Windows
Johnson David
Greenhorn

Joined: May 22, 2005
Posts: 27
thanx jeak nice explenation ,

but what is non-JDK compliant JVM ???


regards

johnson
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31112
    
165

Johnson,
Typically that means a JVM isn't compliant with a specific version of Java. For example, you could have a non-1.3 compliant JVM in that it doesn't meet the specs and pass certification with Sun.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Supratim Das
Greenhorn

Joined: Oct 28, 2012
Posts: 5
Can someone please explain me this....??

"non-JDK compliant JVM, such as the one provided by Microsoft"

Java was developed by Sun Microsystem and currently owned by Oracle Corporation.......So, how is Microsoft coming into the picture ??
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Supratim Das wrote:Can someone please explain me this....??

"non-JDK compliant JVM, such as the one provided by Microsoft"


I am not sure, just guessing here: non-JDK compliant JVM might be the one which does not fully comply to JVM specification defined by Oracle.


Supratim Das wrote:
Java was developed by Sun Microsystem and currently owned by Oracle Corporation.......So, how is Microsoft coming into the picture ??


It was a past story when the software giant has its own VM for Java.


PM Me If You Want to get Urgent Help on Java Programming
Supratim Das
Greenhorn

Joined: Oct 28, 2012
Posts: 5
thanks.......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: driver loading differences