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
- add condition in scope of finally
- move declaration con varialbe to out of scrope.
It still cannot catch any exception
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