可调用语句错误:无法取消引用 void



我正在尝试使用 CallableStage 调用存储过程,但是当我调用它的方法时,它会返回

错误:(35, 62( java: void 无法取消引用。

法典:

public class Connect {
public void connDB() throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
try (Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@test123:4321:test1", "test", "test")) {
if (conn != null) {
System.out.println("Connected to the database!");
} else {
System.out.println("Failed to make connection!");
}
} catch (SQLException e) {
System.err.format("SQL State: %sn%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
}
}
public void callablestatement(int resync_value) {
Connect conectivitateDb = new Connect();
CallableStatement stmt = conectivitateDb.connDB().prepareCall("{call resync_entity.resync_this_entity( ?) }");
stmt.setInt(1, resync_value);
stmt.execute();
}
}

如果我在 conndb 方法的 try catch 块中执行可调用语句,它工作得很好。

您需要从void方法返回Connection以像conectivitateDb.connDB().prepareCall一样链接调用 - 然后您将无法关闭Connection因此这样做会导致明确的连接泄漏。您可能应该将Connection传递给您的方法,并确保至少打印您收到的任何异常。像这样,

public void callablestatement(Connection conn, int resync_value) {
String qry = "{call resync_entity.resync_this_entity(?)}";
try (CallableStatement stmt = conn.prepareCall(qry)) {
stmt.setInt(1, resync_value);
stmt.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}

并且不要忘记关闭呼叫者的Connection

最新更新