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.
So far in this chapter I have described how to create the JavaBean class to represent a single row of data and how the insert new data into a database table. Now I'll show how to update existing data. Working in a version of my project called UpdateSQL, I have added a new method to the AdminManager class. The method is named Update. It receives a single argument, data typed as the Admin JavaBean, and it has an SQL statement. The SQL statement follows the standard for all updates in SQL.
It starts with the keyword UPDATE and then the table name admin and then SET, then there's a series of column name value pairs. I'll be updating two of the values, the userName and password, and then there is a WHERE clause that filters the statement so that these changes are only applied to a single row. This method already has code to create the connection and the prepared statement. I'll add code now to pass the values from the Bean argument into the prepared statement. I'll place the cursor after the opening brace and before the return statement and make a little bit of extra space.
The SQL statement has three parameters, two strings and one integer. So I'll call the setString method twice and the set int method once. I'll start with the strings. I'll call the statement object setString method, I'll pass in a parameter index of 1, and I'll get the value of the username from the JavaBean object calling bean.getUserName. Now I'll duplicate that line of code, and for the second version I'll change the parameter index to 2, and I'll change the method I'm calling to get the value from get.UserName to getPassword.
I'll duplicate it one more time. And this time I'm going to call setInt instead of setString. I'm going to be updating this parameter for the adminID, and that's an integer column in the database. I'll set the parameter index to 3, and I'll call the method getAdminID to get the primary key value from the JavaBean object. So now my prepared statement is ready to go. As I did in the previous video with the Insert statement, when I call execute update, I'll get back in the integer indicating how many rows are affected.
With an update statement where you're filtering on a primary key you should always get back a value of one. So I'll create a variable called affected, and I will call the statement object executeUpdate method without passing any arguments. Then I'll check the affected value. If it has a value of one, I'll return true indicating to the calling scope that the update was successful, and I'll put in an else clause and return false, because if I get back a value other than one that means something went wrong. Now that I have handled all possible cases both the execute update method working and either updating or not updating a row, my final return true is no longer needed, so I'll delete it.
And that's my completed update method. So again, to review it, I'm receiving an object a JavaBean object. I have an SQL statement with placeholders for the values I'm updating and the primary key I'm filtering on and creating a connection and a prepared statement, I'm setting each of the values from the bean to the statement and then executing the update, and then evaluating whether it was successful and returning true or false. Now I'll go back to my main class, and I'll add code to update an existing row. In this version of my main class I'm allowing the user to enter a primary key value in AdminID, and I'm retrieving that single row from the database using the existing method get row.
I'll place the cursor after this if clause. If I get to this point, then I know I'm working with a valid row. I'll ask the user to enter a new password value. I'll use the InputHelper's getInput method, and I'll prompt Enter new password. Notice I'm only doing this after I have verified that they selected an existing row. I don't do this before I get the row, because I might not have valid data to work with. Now I'll take the value that the user entered, and I'll pass it into the bean object using bean.setPassword, and I'll pass in the new password value.
Then I'll execute the update and check the success of the operation all at the same time using if AdminManager.update, and I'll pass in the bean object. And if I get back the true value, I'll do a little bit of system output, and I'll put the line Success. Then I'll add an else clause and within that section, I'll output an error of whoops. And now I'm ready to test my code. I'll run the application, and I'll see that I have an existing row with a primary key of 2 and a username of New Admin and a password of New Password.
I'll enter 2 as the row I want to update. I get the prompt to enter the new password, and I'll enter just pwd. And I'll press Enter, and I get back the message Success. I'll run the application again, and this will show me the existing data now coming from the database table, and I see that new value was successfully updated. I'll try it again, typing in the number of 2, and I'll return it back to New Password, and I could go over to phpMyAdmin and check it there, but if you're seeing it updated and displayed each time you run the application here, then you'll have confidence that the data is being successfully updated in the backend database.
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.