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.
I have shown so far how to do inserts, updates, and deletes using SQL statements and the prepared statement object. There's an alternative in JDBC the updatable result set, where you get a result set from the database and market as updatable, then change the data directly in the result set. You can do inserts, updates, and deletes with this approach. Whether it's a good idea depends on your circumstances and your database management system. Not all database management system supports updatable result sets, and if you're working with a very large result set, it can cause performance issues.
But in a small-scale application, or when you're only dealing with one row at a time, an updatable result set programming approach can be useful, I'll demonstrate how to update a row of data using an updatable result set. I'm working in a project called UpdatableRS. The state of this project is similar to the update exercise that I did previously, there is an update method with an SQL statement, it has three placeholders, two strings for the username and password, and one integer for the admin ID, a prepared statement is being used and executed to make the update.
I'm going to change this code to use an updatable result set instead. First, I'll change the SQL statement. Instead of an update statement, I'm going to use a select, it'll look like this SELECT * FROM admin and then I'll filter on the primary key that's been passed in as part of the bean object. In my SQL statement I'll create a placeholder for that value. Next, I'll go to my prepareStatement method, this is where you'll mark the result set as being updatable. I'll make a little bit of extra space by moving the SQL parameter down to the next line, then I'll add two more arguments.
First, ResultSet.TYPE_SCROLL_INSENSITIVE and then ResultSet.CONCUR_UPDATABLE. This is called the concurrency setting of the result set object, because I have set it as updatable I can now make direct changes to the result set object. My SQL string now only has one placeholder, so I no longer need these two strings, I'll comment them out. And I'll change the parameter index for my final setting to one, so now I have prepared the statement, and I'm going to retrieve the data from the database.
Instead of executeUpdate, I'll be using executeQuery, the same method that I have used previously to retrieve data. And instead of returning an integer, it's going to return a result set. Because I'll be manipulating this result set within my try block, I'll first declare it outside the try block and then close it in a finally block. I'll move the cursor up to above the try block and declared the result set, making sure to import the version from java.sql, I'll name it RS and set it initially to null. Then I'll move down to below the try catch block and add a finally block and within the finally block I'll check to see if it's still null, and if it isn't, I'll close it.
Then I'll go back to the call to statement. executeQuery, and instead of getting an integer, I'll be getting back the result set. I'll delete these remaining three lines of code, they no longer apply, and now I'll add an if else clause. In order to modify the data that I just retrieved, I have to move the result set cursor onto the row of data. So I'll add an if and an else block, and I'll set the condition to rs.next, moving the cursor to the only available row of data. Now I'll update the columns that I want to change.
Even though my application is only going to update the password for the current row, this code doesn't know it. This code should be written so that it updates every column in the database row other than the primary key. So I'm going to call the update string method a couple of times. You'll see that there are update methods for all the various data types that a database supports, I'll call updateString, and as with all of the other methods the sets and the gets and similarly to the get methods that are a part of result set, you can reference a column either by its index or by its name.
I'll choose the version that looks for the name, and I'll pass in username, and then I'll set the value to bean.getUserName. I'll duplicate that line, and for the second version, I'll set the column name to password, and I'll call the bean objects getPassword method. Once I have set those values, I have to commit them by calling a method called updateRow. Notice that there are a few different versions. Because I have already made sure that the cursor of the result set is on the right row, I can use the version of this method that's the simplest, the one with no arguments.
Then because I have gotten through all this code without throwing an exception, I can assume that the update was successful and return true. And if the next method return false, that is if there was no data to update, I'll return false. So this is the final version of the update method using an updatable result set. You'll see that you aren't saving any code by going with this approach, but some developers prefer this to doing update statements. It's really up to you, and whether it works well within your database management system is a matter of a lot of different factors.
If you're not sure whether this is a good idea with your database management system, chat with other developers who have used both the updatable result set and classic SQL statements with your database. But let's complete the exercise. I'll save the changes, and I'll go back to my main class. I'm not going to change anything about the main class. From the main classes point of view, everything should work exactly the same. I'm accepting a primary key value from the user, I'm retrieving a row of data as a Java bean object, I'm applying a new value to the password, and then calling the update method of the admin manager class.
I'll run the code, I see that I only have one row in the database right now, so I'll type in the primary column value of one, I'll enter a new password of new pwd, and I'll press Enter or Return, and I get back the message Success. And if I run the application again, I'll see that the value is correctly updated in the backend database. So it's your choice, updatable result sets or classic SQL statements. Either way you'll be using the prepared statement object to put your code together, and it's really up to you which style you prefer.
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.