无法将 Statement 声明为实例变量
Cannot declare Statement as instance variable
我似乎无法将以下内容声明为实例变量。
public Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
如果在 method/test 方法中声明,那么它工作正常。
"Default constructor cannot handle exception type SQLException thrown by implicit super constructor. Must define an explicit constructor" 是抛出的错误,我也尝试在另一个 class 中声明它并从那里检索它。
感谢任何帮助。
失败的不是声明 - 这是尝试使用可以抛出已检查异常的表达式来初始化变量。
您应该声明该字段,然后在构造函数中对其进行初始化。该构造函数必须声明它抛出 SQLException
(或自己捕获异常)。
作为旁注,我强烈建议避免使用 public 个字段。
例如:
public class Foo {
private final Statement statement;
public Foo(Connection connection) throws SQLException {
statement = connection.createStatement(...);
}
}
我似乎无法将以下内容声明为实例变量。
public Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
如果在 method/test 方法中声明,那么它工作正常。
"Default constructor cannot handle exception type SQLException thrown by implicit super constructor. Must define an explicit constructor" 是抛出的错误,我也尝试在另一个 class 中声明它并从那里检索它。
感谢任何帮助。
失败的不是声明 - 这是尝试使用可以抛出已检查异常的表达式来初始化变量。
您应该声明该字段,然后在构造函数中对其进行初始化。该构造函数必须声明它抛出 SQLException
(或自己捕获异常)。
作为旁注,我强烈建议避免使用 public 个字段。
例如:
public class Foo {
private final Statement statement;
public Foo(Connection connection) throws SQLException {
statement = connection.createStatement(...);
}
}