Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Whether you're building a web- or desktop-based application with Java SE or Java EE, many Java applications need to integrate data from a relational database. This course describes how to read and manage data from relational databases such as MySQL and SQL Server using the Java Database Connectivity (JDBC) API.
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.
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 automatically for you when the try catch block is executed.
You won't need the finally block at all. This works because the kinds of objects that you can place into the try-with-resources section include any object that implements interfaces called Closeable or AutoCloseable. Let's take a look at the documentation for the Connection interface as an example. I'll click on the word Connection, then I'll go to Dynamic Help, I'll go into the Java docs for the Connection interface, and I'll show that this connection extends an interface called AutoCloseable. The AutoCloseable interface, which has many sub interfaces and many implementing classes, has just a single required method called close, which returns void.
So any object that implements this interface or implements closeable which has the same requirements can be added to a try-with-resources block, and if you take a look at the Java docs for the ResultSet, the Statement, and other similar JDBC classes and the interfaces, you'll find that they all have this close method, and that's because they all implement the interface AutoCloseable or Closeable. So, let's go back to the code. Here's how I'm going to clean this up. In Java 7, I'll expand my editor, and then I'm going to move these three lines of code that are declaring my objects into the try-with-resources section.
I'll cut and paste them, and place them within the parentheses. Now I'm going to take the logic that's instantiating these objects and move it up as well. First, I'll do with the Connection. I'll grab the code that's calling the getConnection method, I'll cut it, and I'll replace the null value with that call, then I'll make the same sorts of changes for the statement. I will move the call to create statement to where the statement is being created, and I'll move the call to execute query to where the ResultSet is being created, then I'll delete these three lines of code which are no longer needed.
Now, because these objects have been placed within the try-with-resources section, when the application is finished executing the try catch block, their close methods will be called automatically, and if they're null-- that is if they haven't been created for some reason--they won't throw errors. And that means that I don't need this finally block. I'll select that code, and I'll delete it. I'll select all of my code and go to the Source menu and choose Correct Indentation. Because I have made so many changes, I want to make sure that everything is formatted well. I'll save my changes, and I'll run the application, and I'll see that just as before I'm successfully connecting to the database, creating the statement and executing the query and getting back data from the database.
So, if you're working with Java 7, you can significantly reduce the amount of code that's required to connect to your database and execute SQL statements. If you're working with Java 6 and previous versions, you'll still need to use the older syntax, declaring the objects first and setting them to null and then instantiating them in a try block and closing them in the finally block. And if you're working in Android, as of the date of this recording, Android still follows the rules for Java 6, and so you won't be able to use this try-with-resources syntax there either, but in Java 7 things have been improved significantly, and I encourage you to try it in your own code.
There are currently no FAQs about Java Database Integration with JDBC.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.