Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In a previous video I described how to create a JavaBean class to represent the structure of the database table. The Admin class has one private field for each column of the Admin table and the datatypes of the class matched the datatypes of the columns. There are private fields and public getters and setters. The names match the JavaBean convention where the getters start with the word get and the setters with the words set and then the rest of the names of the methods match the fields. The fields can match the database column names, and they do here, but they don't have to.
Now I'm going to show how to use this JavaBean class to represent a single row of data retrieved from a table. I am working in a version of my project called RetrieveBean, and in this version of the project my AdminManager class has a new method called getRow. The getRow method receives a single argument datatype as an integer. I have named it to match the database column name Admin ID, and that's the primary key column of the database table. Then there is an SQL variable, a string that has the statement I'm going to execute with a question mark for the placeholder. That value will be provided by the end user.
Then I'm creating a result set, a connection, and a prepared statement. I'm taking the value passed in, in the argument and setting it for the placeholder and then executing the query. But for the moment I'm just returning null. Now, I'm going to replace the return statement with the logic that takes the data received from the query and wraps it in an instance of my JavaBean. I will move the cursor after the call to execute query, and I'll add logic to test whether I got back data. If the query returns one row of data, I will be able to move the cursor from its initial position before the first row to the first row by calling the result sets next method.
So I will use an if statement, if rs.next. If this returns true, it means I got data back. But if it returns false, that means that the user provided a value that doesn't match anything in the database table. I'll move my return statement to within the else clause, and I'll also output an error, No rows were found, now I will add logic to wrap up the data that was found inside the if statement. So if the next method returns true, then I have data to work with. I will create an instance of my Admin class.
Remember that it's in the beans package, and you do need an import for it. I will name this new object bean, and I will instantiate it with the classes No arguments constructor method. Now I will set its fields using its setter methods. First, I will set the primary key bean.setAdminId, and I will just use the value that was passed in as the argument. Next, I will set the two string fields, username and password, and I'll use the values from the result set. I will call bean.set username, and I will pass in rs.getString, and I will identify the column I want by its name, userName. Then I'll set the password field.
Once again, I'm using the setString method. For a database table that has more columns, simply call as many versions of the set method as you need to and use the appropriate methods of the result set to get the datatypes that you need. Get string for strings, get double and get int for integers and doubles, and so on. Now my bean object is complete. I have instantiated it, and I have set its values, and I will return it to the calling context. And that's the complete method. I will review all of the code.
The primary key value is passed in as an argument, the SQL statement has a placeholder for that primary key value, I am using a prepared statement object so I can pass the value in using one of the setter methods, and I am executing the query. If I get data back, I wrap that data in an instance of my JavaBean class and return it, if I don't get data back, I return null. Now, let's go to the main class and call this method. I will place the cursor after the call to the AdminManger's displayAllRows method, and I will add code that lets the user indicate what row they want to work with.
I will declare a new variable called AdminId, and I will get its value from a new method of the InputHelper class called getInteger input. I will set a prompt of Select a row. Now I will use that value to get a JavaBean object representing the matching data. I will declare a new JavaBean object, and I will use the Admin data type, and I will set the name of the object as bean, and I will instantiate it using AdminManager.getRow, and I will pass in the AdminId. Now I will test whether I got data back from the query.
I will use an if else clause. I will set my condition to if, bean has a value of null. So if the bean is null, that means I didn't get any data back. In my output manager's method, I am already outputting an error message. I am going to move that to the main class. I will cut it from here and paste it into my main class in my if clause. But then in the else clause, if I did get data back, I will display that data's information. I will use a series of system outputs.
The first one will have a label of AdminId and a call to the getAdminId method of the JavaBean object. I will duplicate that line of code a couple of times and then I will change the labels and the methods that I'm calling. For the second one, I will use UserName as the label, and I will call getUserName. And for the third one, I will set the label to Password, and I'll call getPassword. So now I am ready to test my application. I'll make sure that I saved all my classes, then I will go back to my main class and run the application, I'll type in a value of 2, which won't match row, and I'll get back the error message, No rows were found.
But then I'll run the code again, and I'll pass in the correct value of one, and I get back the data. So now I have an object that I can pass around the application. I can get the data object from one method and pass it into another and in the next set of videos I'll show you how to use this object to manage updates and deletes of existing data.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92190 Viewers
82 Video lessons · 104058 Viewers
71 Video lessons · 75386 Viewers
56 Video lessons · 106759 Viewers
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.
Your file was successfully uploaded.