I cannot seem to be able to declare the following as an instance variable.
public Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
If declared inside a method/test method then it works fine.
"Default constructor cannot handle exception type SQLException thrown by implicit super constructor. Must define an explicit constructor" is the error thrown, I've also tried declaring it in another class and retrieving it from there.
Any help appreciated.
It's not the declaration that's failing - it's the attempt to initialize the variable using an expression which can throw a checked exception.
You should declare the field, then initialize it in a constructor. That constructor will have to declare that it throws SQLException
(or catch the exception itself).
As a side note, I'd strongly suggest avoiding public fields.
For example:
public class Foo {
private final Statement statement;
public Foo(Connection connection) throws SQLException {
statement = connection.createStatement(...);
}
}
See more on this question at Stackoverflow