Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Managing data entities with JavaBean classes

From: Java Database Integration with JDBC

Video: Managing data entities with JavaBean classes

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.

Managing data entities with JavaBean classes

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.

Show transcript

This video is part of

Image for Java Database Integration with JDBC
Java Database Integration with JDBC

32 video lessons · 6215 viewers

David Gassner
Author

 
Expand all | Collapse all
  1. 5m 23s
    1. Welcome
      53s
    2. What you should know before starting this course
      1m 20s
    3. Using the exercise files
      3m 10s
  2. 12m 2s
    1. Testing your Java development environment
      5m 14s
    2. Importing a MySQL database
      5m 1s
    3. Creating a database user in MySQL
      1m 47s
  3. 32m 47s
    1. What is JDBC?
      4m 26s
    2. Choosing a JDBC driver
      6m 46s
    3. Connecting to a MySQL database server
      8m 7s
    4. Connecting to a HyperSQL database file
      6m 23s
    5. Executing a static SQL statement
      7m 5s
  4. 17m 42s
    1. Connecting to multiple databases
      6m 24s
    2. Handling JDBC exceptions
      7m 7s
    3. Closing database resources in Java 7
      4m 11s
  5. 47m 25s
    1. Looping through result sets
      8m 23s
    2. Moving the cursor in scrollable result sets
      5m 51s
    3. Limiting the number of fetched rows
      6m 57s
    4. Filtering data with prepared statements
      6m 58s
    5. Calling stored procedures
      5m 48s
    6. Handling multiple values from stored procedures
      5m 54s
    7. Using generic getter methods in Java SE 7
      7m 34s
  6. 45m 23s
    1. Managing data entities with JavaBean classes
      5m 0s
    2. Retrieving a single row as a JavaBean object
      6m 5s
    3. Inserting rows with prepared statements
      8m 2s
    4. Updating rows with prepared statements
      5m 4s
    5. Deleting rows with prepared statements
      4m 9s
    6. Managing data with updatable result sets
      6m 6s
    7. Using a persistent database connection
      6m 43s
    8. Committing and rolling back transactions
      4m 14s
  7. 9m 35s
    1. Getting the DatabaseMetaData object
      3m 40s
    2. Getting column and data type information
      5m 55s
  8. 50s
    1. Next steps
      50s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Java Database Integration with JDBC.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.