1

如果我的代码捕获异常,我会尝试返回 false,但是当它捕获异常时似乎会停止。

这是我的代码:

第一种方法

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 ++;
        }

第二种方法:TestDb 方法,一种放入可达变量的方法。

public boolean TestDB(Connection conn) throws Exception{

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

}

编辑:这是我的错误

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)
4

7 回答 7

1

我通过在

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

关于第一种方法。它看起来像这样:

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

这是因为错误不在于第二种方法。错误是连接的声明。

于 2013-09-16T11:46:19.360 回答
1

难道只是抛出异常的是DriverManager.getConnection。因为现在编写代码时,您只能捕获 conn.isClosed() 抛出的异常。

于 2013-09-16T10:42:05.900 回答
1

因为在你的 try catch 块之前,发生了一个异常,并且 try catch 块根本无法捕捉到任何异常,可能你想在 while 循环中捕获代码,因为如果你给出方法 getConnection 总是抛出一个异常url、端口、用户名或密码错误

于 2013-09-16T10:42:28.057 回答
0

您是否尝试过删除Throws Exception

改变

public boolean TestDB(Connection conn) throws Exception{

public boolean TestDB(Connection conn){
于 2013-09-16T10:57:41.983 回答
0

我同意 peter Lawrey 的观点。您是否尝试在调试模式下运行并检查了 TestDB 方法中发生了什么?

您是否也可以在您的 catch 块中使用堆栈跟踪并在此处共享完整的堆栈跟踪。

顺便说一句,您是否在第一种方法中处理异常,即 while 循环所属的方法?

于 2013-09-16T10:43:12.750 回答
0

首先纠正你的 if 条件

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

这样

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

如果您的 if 语句为空,则它可能会引发错误conn.isClosed()

但是,您有 catch 异常并尝试使用return;而不是,return false;您可以放置​​您的 stackTrace,以便可以识别出确切的错误。

于 2013-09-16T10:53:51.087 回答
0

您可以使用此语句,而不是您编写的许多行。

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

这将简化代码。最后,我认为您应该使用包含所有代码的大型 try-catch,因此所有内容都经过异常检查

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
    }
    }
于 2013-09-16T11:59:40.000 回答