Chapter 9. Statements
Now that you have a firm understanding of
how to create a Connection
object for each of the
four types of clients outlined in Chapter 1, and
you have the DDL to create the example HR database to use as a
context for the chapters on relational SQL, we’re ready to
change our focus from the Connection
object to the
Statement
object. The Statement
object, which you’ll create using a
Connection
object, allows you to execute both
Data Definition Language (DDL) and
Data Manipulation Language (DML)
statements. The Statement
object is the most
dynamic of the JDBC objects, because you can use its
execute( )
method to execute any valid SQL
statement. If you use the execute( )
method, you
can use its return value at runtime to determine whether there is a
result set and then use the Statement
object’s getResultSet( )
method to retrieve the result set, or
you can use the Statement
object’s
getUpdateCount( )
method at runtime to determine the
number of rows affected by your statement. For most situations,
however, you won’t need that much flexibility. Instead,
you’ll need to insert rows into a table, update or delete rows
in a table, or select rows from a table. To that end, you’ll
most often use one of the Statement
object’s
other two execute methods,
executeUpdate( )
and
executeQuery( )
.
In this chapter, we’ll start by covering how to create a
Statement
object from a
Connection
object. Then we’ll see how to use
the execute( )
method to execute the DDL from
Chapter 8 ...
Get Java Programming with Oracle JDBC now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.