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
Statement needs to be closed when finished, meaning it belongs with the other resources in our
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
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!
The first step is to generate the
ResultSet. Inside the
.loadAllCustomers() method, after we’ve created the
Statement, declare a new
results, and set it equal
statement.executeQuery("SELECT * FROM CUSTOMERS;"). You’ll also need to import
try block, under the comment “Add logic to print the ResultSet here:”, add an empty
while loop. It will iterate while
- Add a print (not a
println()) statement that prints
"Current Customer: "
- Add an empty
forloop that begins at
1and runs up to and including
- After the
forloop, add a
.println()statement that says
"moving to the next customer..."
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
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
projectsfolder in the terminal (use ‘cd’ to change directories).
- Use the command
javac $(find . -name '*.java')to compile all
.javafiles in all subdirectories of the
- Run your program with the classpath variables like before:
java -classpath .:../sqlite-jdbc-184.108.40.206.jar viewmodels.BusinessLogic. Voila!!!