1
votes

Android App is connected to database by using JDBC library. But DriverManager.getConnection("url") is returning null. So I tried putting "try...catch " like this:

      Connection con = null;
      try {

            con = DriverManager.getConnection("dummy url");
            System.out.println("Connected.");

      }catch(ClassNotFoundException ce){
            System.out.println("ClassNotFoundException ");
      }catch(SQLException se){
            System.out.println("SQLException ");
      }catch(Exception e){
             System.out.println("Exception ");
      }finally {
            if (con== null) {
               System.out.println("Result: null");
            }else{
               return con;
            }
      }

Output:

      Result: null

If URL is wrong , Try...Catch should be cached some exception. But it skipped all exceptions and getConnection("dummy url") returned null.

Although I tried to change to real url however the problem is not different.

Updated: I modified some code

  1. add condition in scope of finally
  2. move declaration con varialbe to out of scrope.

It still cannot catch any exception

1
Your scope is wrong.AxelH
What URL are you using for DriverManager.getConnection(...). It is obviously throwing some exception. Did you try to debug your code and see which exception is it throwing? But, what I don't understand is, there has to be one of the exception statements from your catch clauses before the line within the finally block System.out.println("Result: "+ con). Also, since you are using this for Android (given that you have put Android Studio as one of the tags), try to see this link [developer.android.com/reference/java/sql/DriverManager] to check what you might be doing wrong.Ashish
Its throwing exception. Check the screenshot ibb.co/WnpJXys And of course as all said your scope is wrongMak
Mark.Thanks for your screenshot. But I never get this exception when I run a debug in Android Studio.Fame th
You really shouldn't be using JDBC from an Android app.Mark Rotteveel

1 Answers

3
votes

That con variable declared in the try block is out of scope in the catch and finally clauses.

The only way that this code could compile is if you have declared another con variable ... probably as a field of the enclosing class.

This explains why you are seeing con with a null value when that apparently cannot happen. You are printing the other con variable.


Note that DriverManager.getConnection(...) won't ever return null. The javadoc states that it returns a Connection object, and nothing else. If the method could return null, the documentation would say so explicitly.


UPDATE Once you have corrected the scoping problem with con. It is now possible that the problem is that you are not catching the exception. Specifically, it could be an Error exception, which might arise if there was a problem loading (say) the appropriate JDBC driver class.

Change

  }catch(ClassNotFoundException ce){
        System.out.println("ClassNotFoundException ");
  }catch(SQLException se){
        System.out.println("SQLException ");
  }catch(Exception e){
         System.out.println("Exception ");

to

  } catch(Throwable e) {
         e.printStackTrace();
  }

(If you catch an exception for diagnostic purposes, you should always print or log the stacktrace!)