Can I declare them only at one place as class members (instance variables)?
You can do so, but the class would be threadunsafe. The caller can't reuse the same instance across multiple threads without disturbing the behavior of each individual method caused by a inconsistent state. In case of a servlet as the caller, this way you can't create the instance only once in servlet's init()
and reuse it multiple times in the doXxx()
methods. You would be forced to re-create the instance in threadlocal scope (so, inside the doXxx()
method). This should be clearly documented in the class' javadoc. But, after all, designing a threadunsafe DAO class doesn't make sense. Stick to the current design (or, if you weren't just hobbying around, switch to JPA ;) ).
Can doing so maintain a consistent state precisely?
No! On the contrary, it would be inconsistent. You can't share the one and same statement or resultset across multiple queries. Each query should have its own statement and resultset. Every method call on the instance would change the instance variables, causing the other still-running method calls to work on a corrupted state. Sharing a connection is possible, but this job is normally already done by a connection pool. Given that you're using a datasource, you most likely already have one.
That said, if you hate the repeated code boilerplate but really want to stick to good 'ol JDBC API, look at Execute Around pattern/idiom and/or Java 7's automatic resource management (ARM). With this, it must be possible to create a helper DB class with a bunch of interfaces and end up with a generic abstract base DAO class whose methods only take a SQL query and the parameter values -if any.