Join David Gassner for an in-depth discussion in this video Closing database resources in Java 7, part of Java Database Integration with JDBC.
- View Offline
As I have previously described in this video series, JDBC resources need to be closed when you're done with them.…The Connection, the Statement, and the ResultSet are examples of these resources.…In class of JDBC code, which I show in this project Java7Closeable, you declare your resources…first and initially set them to null, then you actually instantiate them within a try…block, as I have done here.…And then you close them in a finally block first, making sure that they're not null so…that you don't cause yet another error.…
Now all of this code can feel a little bit cumbersome, but in Java 6 and prior it's simply required.…Java 7, however, gives us a new syntax called try-with-resources, which when you apply it…to these JDBC resources, can clean up your code significantly.…The try-with-resources syntax looks like this.…You add code after the try keyword and before the code block, create a set of parentheses,…and then you can instantiate your JDBC resources within those parentheses, and they'll be closed…
Author David Gassner explains how to choose a JDBC driver and connect to one or more databases. He also provides detailed instructions on reading, selecting, and updating data; calling stored procedures; managing data via JavaBean classes or with prepared statements; and working with metadata.
- Importing a MySQL database
- Connecting to databases
- Handling JDBC exceptions
- Looping through result sets
- Limiting the number of fetched rows
- Filtering data with prepared statements
- Calling stored procedures
- Inserting, updating, and deleting rows with prepared statements
- Using a persistent database connection
- Committing and rolling back transactions