Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

driver loading differences

 
Johnson David
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Johnson David
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx jeak nice explenation ,

but what is non-JDK compliant JVM ???


regards

johnson
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Supratim Das
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Supratim Das
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic