I am not sure, which method is better?
con.createStatement().execute("...");
OR
Statement st = con.createStatement();
st.execute("...");
st.close();
When using the first method, will be the statement create closed or it will remain open? I am sure that in the second method the statement will be closed, but in the first one? Anybody know?
Both con.createStatement().execute("...");
and Statement st = con.createStatement();
make same sense. However closing the statement is not performed with the first method. Closing the ResultSet
or Statement
is a good practice. You may get additional problems with not closed Statements
. You can close the Connection
instead, but it's not possible when using a connection pool. Additionally the second method is very useful when using the PrepairedStatement
, because it can set parameters to the PreparedStatement
.