Solved [SQL] Should I close the statement?

Discussion in 'Spigot Plugin Development' started by Gustavo_kio, Mar 20, 2020.

  1. Hello guys, my question is: Can I just close the ResultSet? Or should I close the statement too?

    I'm currently have a method that looks something like this:
    Code (Text):
    public ResultSet executeQuery() throws SQLException {
            PreparedStatement stm = connection.prepareStatement(sql);

            return stm.executeQuery();
    I'm passing the resultset which means i can't close the statement rn, but if it's really necessary (prevent performace or error issues) i'll pass the statement and close it on the other end.
  2. Choco


    A ResultSet will be automatically closed when a Statement is closed. But close your resources. This is how resource leaks occur.
  3. Thanks. But i know that. What about the opposite? If i close the resultset will it close the statement? (no right?)
  4. Choco


    Correct. If I recall correctly, you can close the ResultSet without closing the Statement.
  5. Too bad. Thank you!