I have a question for understanding JDBC.

Statement: is it possible to run the next query through one Statement without closing the previous one?

  • And you try. you create one, you create the second, you execute the second, you execute the first, if you want. - arg

1 answer 1

It is possible, but it is not worth doing.

(extract from java-docs)

/** * <P>The object used for executing a static SQL statement * and returning the results it produces. * <P> * By default, only one <code>ResultSet</code> object per <code>Statement</code> * object can be open at the same time. Therefore, if the reading of one * <code>ResultSet</code> object is interleaved * with the reading of another, each must have been generated by * different <code>Statement</code> objects. All execution methods in the * <code>Statement</code> interface implicitly close a statment's current * <code>ResultSet</code> object if an open one exists. * * @see Connection#createStatement * @see ResultSet */ public interface Statement extends Wrapper, AutoCloseable { 

But if we are talking only about the correct work with resources, then we need to look towards using JdbcTemplate