At the end of the last example, we made a Connection object, representing our physical connection to the database. Now we can use this Connection object to create Statement objects. Statement objects are JDBC's way of getting SQL statements to the database. There are three main types of Statement: the base class Statement, PreparedStatement, and CallableStatement. We'll begin by discussing the first one, for now.

To create a Statement object, use the following code:

Statement s = c.createStatement();

You can now use this Statement object to execute queries and updates on the database. There are two main methods in Statement that are important. The first is "executeQuery". This method takes one argument, the SQL statement to be executed, and returns an object of type ResultSet, which we'll talk about later. This method is used for executing queries which will return a set of data back, for instance, a SELECT . The ResultSet object returned represents the data resulting from the query. The other important method is "executeUpdate". This method, again, takes one argument, the SQL statement to be executed. This statement, unlike the last one, is going to be a statement which changes data in the database: a CREATE , or an INSERT , or an UPDATE . The difference is that this returns an "int", and this corresponds to the number of records that were updated.

Here's an example of selecting some data from our database:

Statement s = null;
try {
     s = c.createStatement();
} catch (SQLException se) {
    System.out.println("We got an exception while creating a statement: that probably means we're no longer connected.");
    se.printStackTrace();
    System.exit(1);
}
ResultSet rs = null;
try {
    rs = s.executeQuery("SELECT * FROM book");
} catch (SQLException se) {
    System.out.println("We got an exception while executing our query- that probably means our SQL is invalid");
    se.printStackTrace();
    System.exit(1);
}

int index = 0;

try {
    while (rs.next()) {
        System.out.println("Here's the result of column number " + index++ + ":"); 
        System.out.println(rs.getString(1));
    }
} catch (SQLException se) {
    System.out.println("We got an exception while getting a result out of the set- this should never happen, so we're doing something really bad.");
    se.printStackTrace();
    System.exit(1);
}

Here, we create a Statement object, and then use that Statement object to execute the query "SELECT * FROM book". We get back a ResultSet, and use that ResultSet to print out some of the information we got back.

The ResultSet object is our primary interface for fetching information from the database. It has two main important features. It can step through the set of rows returned, and it can print out values for a specific column in that row. It works in a similar fashion to a standard Java Enumeration- it starts before the first element, and you use the "next" method to step through the rest of the elements. "next" returns true if the ResultSet was able to step to the next results- that is to say, there are results to be read. This while loop will print out the first column of each of the rows returned. If no rows were returned, "next" will return false initially, representing this fact, and therefore nothing will be printed.

ResultSet can return values of all sorts of different types- in this case, we're treating the first column as if it were a String. Fortunately, all standard SQL data types can be represented as Strings, so regardless of the type of the first column, we'll be able to fetch the value of the first column and print it out. There are many other methods available on ResultSet- ones for fetching all the various SQL data types and converting them to native Java types. Consult the API documentation for ResultSet for more information.