views:

184

answers:

5

Here is my code - i am simply checking my MySQL database connection.
But first i have compiled and run the program successfully. but then i have commented the line Class.forName .
Still when i compile it runs successfully, without any error.Why?

import java.sql.Connection;
import java.sql.DriverManager;


public class FirstJbdc {
    public static void main(String[] args) {
     Connection cn=null;
     try {
      //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      System.out.println("Driver loaded successfully");
      cn=DriverManager.getConnection("jdbc:odbc:myDSN","root", "java");
      System.out.println("Database connected successfully....");
      System.out.println(cn);
     } catch (Exception e) {
      // TODO: handle exception
                        e.printStackTrace();
     }
    }
}
+7  A: 

I throws an error, alright. It's just that the

catch (Exception e){
     // here the exception is instantiated, but nothing is done about it
}

clause silently swallows your exception.

Try a

System.out.println( e.getMessage() );

in the catch clause

Steen
Or e.printStackTrace();
pjp
Agree - it runs without (displaying) an error but it does not run successfully (doesn't print the last two lines). Never ever ever (...) catch 'Exception' without handling it ;)
Andreas_D
tried handling exception but still it's running successfully
Webbisshh
@peter - can you give any link related to this!
Webbisshh
@Pradyumna Dandwate: Maybe you can edit the question with your updated code. Not sure what you meant by "tried handling exception."
tim_wonil
I updated my answer with relevant info and link to authoritative documentation.
Peter Štibraný
+7  A: 

Java 1.6 can find JDBC driver even without using Class.forName.

Here is relevant part of documentation:

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:

my.sql.Driver

Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

Peter Štibraný
Hey Peter - please can you point to the docs that detail this, been searching but can't find anything - cheers Nick.
Nick Holt
Don't you have to put the JDBC driver in a special folder for this to work?
pjp
@pjp - would've thought you'd have to do something like putting the driver in a 'special folder'. Otherwise you'd be trawling through every class checking for which ones implement java.sql.Driver. which is gonna suck.
Nick Holt
@Nick: check out Javadoc for [DriverManager](http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html) in JDK 1.6, especially part beginning with "The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism."
Peter Štibraný
@Peter: cheers, that's a tick on my learnt something new today list :-)
Nick Holt
@Nick Holt: ditto. cheers, Peter.
tim_wonil
+1  A: 

NOTE: this only applies to pre-JDBC 4.0 Drivers.

JDBC drivers are meant to have a static section that registers them with the java.sql.DriverManager when the class is loaded, hence the Class.forName(String) is required.

It's detailed here: http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/drivermanager.html

Nick Holt
+1  A: 

try {

     //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      System.out.println("Driver loaded successfully");
      cn=DriverManager.getConnection("jdbc:odbc:myDSN","root", "java");
      System.out.println("Database connected successfully....");
      System.out.println(cn);
} catch (Exception e) {
    // add the following statement
    System.out.println(e.getMessage());
}

If you add the statement inside the catch block, then compile and run, you should see the error message like-

[Some Com][Some Driver Manager] Data source name not found and no default driver specified

ZiG
A: 

Without Class.forName(), the JDBC-ODBC bridge driver is not loaded. By JDBC specification, getConnection() returns null if no driver is found for the URL, no exception is thrown. So this is expected behavior.

ZZ Coder
Except if you're using JDBC 4.0 (Java 6), because there it's not necessary anymore to load the driver with Class.forName(...). See the JavaDoc of java.sql.DriverManager.
Jesper
The JDBC-ODBC driver from Sun is so old. It doesn't know the new tricks.
ZZ Coder
Oh yes, it does. Check out META-INF/services/ava.sql.Driver file inside jre/lib/resources.jar -- it contains "sun.jdbc.odbc.JdbcOdbcDriver", i.e. JDBC-ODBC driver from Sun! So it can be definitely used *without* Class.forName() on Java 6.
Peter Štibraný
You are right. I forgot we use a customized version of the driver.
ZZ Coder