Question

I am trying to return false if my code catches an exception but it seems like to stop when it catches an exception.

Here's my code:

First method

while ((reachable == false) && (cnt <= 2)) {
            serverName = FPRODDB[cnt][0];
            sid = FPRODDB[cnt][1];
            url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
            conn = DriverManager.getConnection(url, username, password);
            reachable = TestDB(conn);
            JOptionPane.showMessageDialog(null,
            "Testing Connection: " + String.valueOf(cnt) ); 
            cnt ++;
        }

Second method: The TestDb Method, the one put in reachable variable.

public boolean TestDB(Connection conn) throws Exception{

try {
    if (conn ==null || conn.isClosed()) {
        return false;
    }else {
        return true;
    }
} catch (Exception e){
       return false;
}

}

EDIT: Here's my Error

java.sql.SQLException: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at digicare.tracking.serial.BulkUpload.DatabaseConnection.OracleConnection.GetDB(OracleConnection.java:45)
    at digicare.tracking.serial.BulkUpload.DatabaseConnection.OracleConnection.main(OracleConnection.java:13)
    at digicare.tracking.serial.BulkUpload.UploadProgress$1read2.run(UploadProgress.java:108)
    at java.lang.Thread.run(Unknown Source)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:328)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:421)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:630)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:206)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
    ... 10 more
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:127)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:126)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:306)
Was it helpful?

Solution 3

I got it working by adding a try block on the

conn = DriverManager.getConnection(url, username, password);

on the first method. it looks like this:

try {
  conn = DriverManager.getConnection(url, username, password);
  reachable = TestDB(conn);
} catch (Exception e) {
   reachable = false;
}

It is because the error is not with the second method. the error is the declaration of the connection.

OTHER TIPS

Could it just be that it's DriverManager.getConnection that throws the exception. Because as the code is written now, you only catch the exception thrown by conn.isClosed().

because before your try catch block ,there is a Exception happening, and ,the try catch block can catch no Expcetion at all,may be u want to catch the code inner while loop, as the method getConnection alwayse throw an Exception if u give the wrong url、port、username or password

I agree with peter Lawrey.and did you try to run in debug mode and checked what exaclty happening in TestDB method?

Can you also use stacktrace in your catch block and share the full stack trace here.

BTW are you handeling exception in first method, the method to which while loop belong?

First of all correct your if condition

if (conn ==null || conn.isClosed())

By this

if (conn !=null && conn.isClosed())

Your if statement may throw error if it is null at conn.isClosed()

However you have catch exception and try with return; instead of return false; and you can put your stackTrace so exact error may identify.

Have you tried removing Throws Exception ?

change

public boolean TestDB(Connection conn) throws Exception{

to

public boolean TestDB(Connection conn){

you can use this statement, instead of the many lines you wrote.

try{
    return !conn.isClosed(); //true if open
}catch(Exception e){
    return false; //is false if nullPointerException is thrown (that mean conn is null)
}

This will simplify the code. At the end I think you should use a big try-catch that contains all the code, so everything is exception checked

while ((reachable == false) && (cnt <= 2)) {
        serverName = FPRODDB[cnt][0];
        sid = FPRODDB[cnt][1];
        url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    try{
        conn = DriverManager.getConnection(url, username, password);
        reachable = TestDB(conn);
        JOptionPane.showMessageDialog(null,
        "Testing Connection: " + String.valueOf(cnt) ); 
        cnt ++;
    }catch(Exception e){
        // tell the user, or handle it
    }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top