Help us understand the problem. What is going on with this article?

Statement と ResultSet の close 整理

More than 1 year has passed since last update.

Statement

注: Statementオブジェクトがクローズされるとき、その現在のResultSetオブジェクトが存在すれば、それもクローズされます。

Statement (Java Platform SE 8 )

ResultSet

注: ResultSetオブジェクトは、このオブジェクトを生成したStatementオブジェクトがクローズされるとき、再実行されるとき、または一連の複数の結果から次の結果を取り出すのに使用されるときに、そのStatementによって自動的にクローズされます。

ResultSet (Java Platform SE 8 )

ということなので、 Statement の方を確実に close() していれば、 ResultSetclose() は書かなくても良いっぽい。

package sample.jta;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("org.h2.Driver");

        try (Connection con = DriverManager.getConnection("jdbc:h2:mem:test", "sa", "")) {
            try (Statement stmt = con.createStatement()) {
                stmt.executeUpdate("create table if not exists foo(id number(10), code varchar(20))");
            }

            try (PreparedStatement ps = con.prepareStatement("insert into foo values (?, ?)")) {
                ps.setLong(1, 1L);
                ps.setString(2, "hoge");
                ps.executeUpdate();

                ps.setLong(1, 2L);
                ps.setString(2, "fuga");
                ps.executeUpdate();
            }

            ResultSet rs;
            try (PreparedStatement ps = con.prepareStatement("select * from foo")) {
                rs = ps.executeQuery();
                while (rs.next()) {
                    long id = rs.getLong("id");
                    String code = rs.getString("code");
                    System.out.println("id=" + id + ", code=" + code);
                }
                System.out.println("(1) rs.isClosed()=" + rs.isClosed());
            }
            System.out.println("(2) rs.isClosed()=" + rs.isClosed());
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }
    }
}
実行結果
id=1, code=hoge
id=2, code=fuga
(1) rs.isClosed()=false
(2) rs.isClosed()=true

PreparedStatementclose されたら ResultSetclose されてる。

追記

ややこしいケースもありそうなので、明示的に close() するのが良さそうです。

opengl-8080
ただのSE。Java好き。
tis
創業40年超のSIerです。
https://www.tis.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした