Use a try-with-resources statement to automatically close a java.sql.Statement object:

Example:
public static void goyunExample(Connection con) throws SQLException {

    String query = "select email from contacts";

    try (Statement stmt = con.createStatement()) {
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            String email= rs.getString("email");
            System.out.println(email);
        }
    } catch (SQLException e) {
        // print exception(e);
    }
}

Comments