Learn

Now that we have connected to the database, it is time to begin communicating with it.

JDBC provides a very straightforward class to take care of executing static SQL queries, the Statement interface. A Statement is generated from a call to the Connection method, .createStatement().

Connection connection = DriverManager.getConnection(url); Statement statement = connection.createStatement();

Once a Statement object is created it can be used to execute SQL queries and return a ResultSet object, which is essentially a Java representation of a table from the database. The ResultSet can then be used in the logic of your program.

These Statement objects, just like Connection objects, also need to be closed when you are finished using them and are best included inside the same try-with-resources block that a Connection is opened in.

There is also an extension of Statement called PreparedStatement that is used to execute a precompiled SQL statement multiple times and with greater efficiency. As your program interacts more and more with the database you may find that a PreparedStatement might improve your performance.

For now, let’s set up our Statement objects that we will use in each of our remaining CustomerDaoService methods.

Instructions

1.

Inside CustomerDaoService.java you will see that we have added the try-with-resources stubs to each of the remaining methods. We’ve added some whitespace to increase readability as well. Declare a new Statement, called statement, and set it equal to the return of calling the .createStatement() method on the connection object, do this for each of the following:

  1. .createTable() method
  2. .saveCustomers() method
  3. .loadAllCustomers() method
2.

Add an import statement to CustomerDaoService for the Statement interface, it comes from the java.sql as well.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?