From the course: Learning JDBC
Reading all data
- [Instructor] So now that we have everything set up, it's time to read all the data from our services table through our services domain. So go ahead and open up your service DAO. Now I'm going to show you the pattern that I use when I'm writing these things because I think it's very important to keep your SQL separate from the actual code itself as much as possible. So the first thing that we're going to do is we're going to go ahead and create a logger here. (keyboard clacking) So now that our logger is done, let's do a private static final string, get all, and I always use the same patterns when I do these. So I don't only put my SQL statements at the beginning of the class file, I use the same basic structure of my methods to keep these things working in a similar way. So now we're going to do our select. Now it's important here to actually name the fields that you're going to get back. So select service ID name price from Wisdom.Services. Again, use that data file, that data.SQL file if you need to. So now let's scroll down to the get all method. Go ahead and remove this comment so we can implement this. So the first thing I'm going to do is I'm going to create a list of service objects and I will simply set this to a new array list. Now there is a case where I throw this away and it becomes a blind object that never gets used. But by doing this, I know that I will always return something that is not null. And I always do that on a get all method. So now I will create an instance of my connection and I'm going to get that from DatabaseUtils.getConnection, Go ahead and import that in. So now I've got a handle to the connection. So now I'm going to do a try statement and so I don't have to worry about closing and I'm going to wrap that in the try. So statement statement equals connection.CreateStatement and now I'm going to get an instance of a result set equals statement.execute query and I'm going to go ahead and pass a get all. Now I'm going to set my services equal to this.processResultsSet and I'm going to go ahead and pass it in that results set. So all of that work that we did before got us to the point of getting a result set. Now I need to do my catch. So I'm going to catch a SQL exception. So if I get an exception, I will go back to DatabaseUtils.HandleSQLException. We are going to do a service DAO.GetAll and go ahead and pass it in E in the logger. And then we will simply return our services. Now, in order to test this, we're going to go back to the app class that came for free with the archetype and we will delete the hello world. We'll create an instance of our service DAO. We'll call this "Service DAO". (keyboard clacking) Go ahead and import that in. And now we're going to create a list of services called "Services" and we will simply set that equal to the service DAO.GetAll. And now we're going to give ourselves a little bit of an indication of what's going on. So we're going to start with a output with four stars. We'll call this "Services" and then we are going to do another output. Go ahead and do a /N three stars get all three more stars. And now we can do a services.forEach and we will do a system.out::printLine. Let's go ahead and import what is missing and we can now run this. So we're simply going to run the main method and you'll see in our output now we actually have services. We've got a get all, and then we have all of our data elements using the two stream method that we put on the entity. We've now connected to the database running in our Code Spaces environment from our application.
Contents
-
-
-
-
(Locked)
Using a driver7m 4s
-
(Locked)
The Data Access Object (DAO) pattern4m 44s
-
(Locked)
Understanding result sets6m 38s
-
Reading all data5m 54s
-
(Locked)
Reading instance data5m 7s
-
(Locked)
Creating data7m 10s
-
(Locked)
Updating data4m 14s
-
(Locked)
Deleting data3m 35s
-
(Locked)
The Repository pattern4m 32s
-
(Locked)
Challenge: Implement a DAO1m 12s
-
(Locked)
Solution: Implement a DAO2m 1s
-
(Locked)
-
-