Everything we’ve done so far has revolved around pushing data to our database, an important skill, but only half of the story.
Let’s take a look at a very common data persistence scenario, loading a user’s previous data when an app is closed and reopened at a later date.
- Just prior to a user exiting an app and the process is killed, the application executes a save function that pushes the user’s current state to a database.
- When the application is reopened in the future, a splash screen is shown, along with a loading bar
- During this time the application is sending a request back to the database to load that user’s saved state, moving the loading bar along as data is received.
- When the splash screen disappears it then appears to the user that the app never forgot where the user left it, when in fact all the information came from a separate database.
Through our JDBC implementation, we can produce similar results, but we need to now learn how to retrieve information back from the database.
In JDBC, the data that is sent back to the Java application from a database comes in the form of a ResultSet
and is returned by a query on the database from a Statement
object. This ResultSet
object, just like Connection
and Statement
needs to be closed when finished, meaning it belongs with the other resources in our try-with-resources
block.
A ResultSet
object is a collection of rows and columns that represent a table from the database. There is a “cursor” that points to the current row, from which we can access columns by the column’s index (which, unlike other Java data structures, begins with index 1).
Statement statement = connection.createStatement(); ResultSet results = statement.executeQuery("SQL QUERY");
You may have noticed in the code example that we are now calling the .executeQuery()
method instead of the .executeUpdate()
method, the difference being that a query returns a ResultSet
.
Once we have injected the SQL into the database and received the result as a ResultSet
, we can use regular ol’ Java code to work through the results and present them to the user.
Let’s see how this looks inside Mystery Business!
Instructions
The first step is to generate the ResultSet
. Inside the .loadAllCustomers()
method, after we’ve created the Statement
, declare a new ResultSet
, called results
, and set it equal statement.executeQuery("SELECT * FROM CUSTOMERS;")
. You’ll also need to import java.sql.ResultSet
.
Inside the try
block, under the comment “Add logic to print the ResultSet here:”, add an empty while
loop. It will iterate while results.next()
.
Inside the while
loop:
- Add a print (not a
println()
) statement that prints"Current Customer: "
- Add an empty
for
loop that begins at1
and runs up to and including5
. - After the
for
loop, add a.println()
statement that says"moving to the next customer..."
Inside the for
loop, use a .print()
statement to print out the current column value (indexed by i
) using the .getString()
method of ResultSet
. Follow this with a comma and a space. Remember the pass the column index, i
, to the .getString()
method.
Navigate to BusinessLogic.java, inside the .main()
method, add a call to the updated .loadAllCustomers()
method of the CustomerDaoService
class under the applicable comment.
Compile and run your program:
- Navigate to the
projects
folder in the terminal (use ‘cd’ to change directories). - Use the command
javac $(find . -name '*.java')
to compile all.java
files in all subdirectories of theproject
folder. - Run your program with the classpath variables like before:
java -classpath .:../sqlite-jdbc-3.36.0.3.jar viewmodels.BusinessLogic
. Voila!!!