When to close Connection, Statement, PreparedStatement and ResultSet in JDBC

Personally I’d use a pool as this will take care of all of the resource management for you. If your connection requirements change then you can easily modify the pool configuration. With a pool in place you can open/close connections and prepared statements according to best-practice and leave the resource management to the pool. Typically, … Read more

Getting java.sql.SQLException: Operation not allowed after ResultSet closed

The problem is with the way you fetch data in getStuff(). Each time you visit getStuff() you obtain a fresh ResultSet but you don’t close it. This violates the expectation of the Statement class (see here – http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html): By default, only one ResultSet object per Statement object can be open at the same time. Therefore, … Read more

How do I iterate over the results in a MySQLi result set?

You will notice while researching the PHP manual at https://php.net/manual/en/mysqli-result.fetch-array.php that fetch_array() has the default behavior of generating a result set that contains both indexed and associative keyed elements (MYSQLI_BOTH). You could use either MYSQLI_ASSOC … while ($row = $output->fetch_array(MYSQLI_ASSOC)) { echo $row[‘uid’]; } or MYSQLI_NUM… while ($row = $output->fetch_array(MYSQLI_NUM)) { echo $row[0]; } That … Read more

Getting the date from a ResultSet for use with java.time classes

Most database vendors don’t support JDBC 4.2 yet. This specification says that the new java.time-types like LocalDate will/should be supported using the existing methods setObject(…) and getObject(). No explicit conversion is required and offered (no API-change). A workaround for the missing support can be manual conversion as described on the Derby-mailing list. Something like: LocalDate … Read more