Join David Gassner for an in-depth discussion in this video Managing data entities with JavaBean classes, part of Java Database Integration with JDBC.
My focus so far has been on weeding and displaying existing data from our backend databases. Now, I'm going to move to how to manage your data, how to insert new rows, and how to update and delete existing rows. I'm working in a version of my project called JavaBeans, and I have made some changes to the classes and how they're implemented. In this version of the project, in the Tables package there are now three classes named AdminManager, StatesManager, and ToursManager. These classes are the manager classes for each of the named tables, Admin, States, and Tours.
Each of them now implements a method called Display All Rows. I'll show you how this is implemented in each of the classes. The Display All Rows method now contains all of the code needed to retrieve the data and display it in the console. There is an SQL variable that's local to the method, then within the try-with-resources block, a Connection, a Statement, and ResultSet are being created. Then there's some looping code to output the data from that table. You'll see the same sort of implementation is available in the AdminManager, the StatesManager, and the ToursManager.
In order to manage data, you need a good way to encapsulate rows. Each row is a data entity, and in Java, we represent data entities with a type of class called a JavaBean. The syntax of a JavaBean is fairly straightforward. A JavaBean class has a name, it has private properties to represent its values, and then public Setter and Getter methods that allow the rest of the application to access and modify those values. I'll show you first how to create a JavaBean class and then how to use it in a couple of different ways.
I'll go back to the Package Explorer, and I'm going to create a new package. I'll right-click on the DB package and choose New > Package, and my new package will have an extension of .beans. In a complete application, I would have one JavaBean class for each database table. For now, I'm just going to create one JavaBean class for the Admin table. I'll right-click on the new package and choose New > Class, and I'll name my class exactly the same as the table, but if the table uses a plural value such as tours or packages, the bean will have a name that's singular, because it's going to represent a single row of the database table, not all of them.
For the Admin table, I'll just use the word Admin. I'll make sure I'm not creating a main method. This class is designed to be instantiated to represent a single row of data and not to launch a full application. Once the class has been generated, I'll create one private field for each column in the table. In my Admin table, the first column is an integer column, and it is named adminId. Next, I'll create a string, and I'll name it username. And finally, I'll create another string, and I'll name it password.
I'm setting the names of my fields exactly the same as my database column names, but you don't necessarily have to do that. Especially in an environment where your database tables might have very long names, you can use shorter field names to make your Java programming a little bit easier. Now, I'm going to generate public methods called Setters and Getters. I'll go to the menu and choose Source, then Generate Getters and Setters. I'll click Select All to indicate that I want methods for all three private fields. I'll set the insertion point for this new code to after the Last member, whatever it might be.
I'll leave the Access modifier set to public, and I'll click OK. And that creates all the methods I need for my JavaBean. I'll clean up my code a little bit, adding a little bit of extra white space and deleting it where it's not needed. And this JavaBean class is now ready to use. One more word about JavaBeans, if you look at the specification for JavaBeans, you'll find that a true JavaBean is supposed to be serializable. You do this by adding the implements keyword and then Serializable. Make sure to include the import statement for the Serializable interface.
The serializable interface doesn't have any methods you have to implement. It's a marker interface. It's just a way of saying to the rest of your environment that this class can be serialized. That is saved to a local persistent environment such as a database or to a local disk. When you mark this as Serializable, you'll see a warning. I'll move my cursor over the warning indicator, and I'll see that it's asking me to create something called a serialVersionUID field. I'll go ahead and create this by clicking on the warning indicator, and adding a generated serial version ID.
This is a long value, and it's used to uniquely identify the class when it's been serialized. Now, the truth is you don't need this code for the kind of application we're building here, And even though the JavaBean spec says that all JavaBeans should do this, if you don't need the functionality for your particular application, you don't need to add it in. So I'm going to delete that, and I'm also going to take away the implements marker and remove the import statement. So, now I have a JavaBean that can represent a single row of my Admin table, and I'll be able to use this JavaBean class to represent data that I want to update and delete in my application.
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