[oracle]恢复后捕捉异常的代码

标签: Java Oracle
发布时间: 2017/4/14 23:54:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想如果我的代码捕获的异常,但它似乎像要停止它捕获异常时返回 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)

解决方法 1:

我得到了它的工作通过 try 块中添加

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

在第一种方法。 它看起来像这样︰

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

这是因为错误不是与第二种方法。该错误是连接的声明 》。

赞助商