try { Connection con = DriverManager.getConnection(url, username, password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM employees"); // Process the ResultSet rs.close(); stmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); }
try { Context initCntx = new InitialContext(); DataSource ds = (DataSource) initCntx.lookup("java:/comp/env/jdbc/mydb"); Connection con = ds.getConnection(); PreparedStatement pstmt = con.prepareStatement("SELECT * FROM customers"); ResultSet rs = pstmt.executeQuery(); // Process the ResultSet rs.close(); pstmt.close(); con.close(); } catch (NamingException | SQLException e) { e.printStackTrace(); }In this example, we are using a connection pool to get a connection object and executing a PreparedStatement. Once we are done with it, we are explicitly closing the ResultSet object. The java.util.ResultSet.close() method is part of the JDBC library, which is built-in to Java.