我已经建立了连接,如以下代码片段所示。我尝试在 catch
block 中再次使用相同的连接,如下所示:
Statement InsertRemoteResultsStmt = null;
try {
Connection connRemote = DriverManager.getConnection("//my urls here");
// other stuff here
}
catch(SQLException ex)
{
InsertRemoteResultsStmt = connRemote.createStatement(); // error comes here
}
我在 Netbeans 中收到错误“找不到符号 connRemote”
。我在这里做错了什么?
最佳答案
你可以这样做:
Connection connRemote = null;
Statement InsertRemoteResultsStmt = null;
try {
connRemote = DriverManager.getConnection("//my urls here");
// other stuff here
}
catch(SQLException ex) {
InsertRemoteResultsStmt = connRemote.createStatement(); // error comes here
}
但是,您无法确定您的连接对象在 catch 子句中是否有效。考虑重新组织代码,例如在 try 子句中嵌套额外的 try/catch 语句。
关于java - 在 catch block 中找不到连接对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22312845/