Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java not giving Error!

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();
        }
    }
}
like image 936
RubyDubee Avatar asked Aug 29 '26 02:08

RubyDubee


1 Answers

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

like image 191
Steen Avatar answered Aug 30 '26 15:08

Steen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!