Java Reference
In-Depth Information
java.sql.SQLTransientException
-rooted class hierarchies, where
SQLNonTransientException
describes failed operations that cannot be retried
without changing application source code or some aspect of the data source, and
SQLTransientException
describes failed operations that can be retried immedi-
ately.
Statements
After obtaining a connection to a data source, an application interacts with the data
source by issuing SQL statements (e.g.,
CREATE TABLE
,
INSERT
,
SELECT
,
UPDATE
,
DELETE
, and
DROP TABLE
). JDBC supports SQL statements via the
java.sql.Statement
,
java.sql.PreparedStatement
, and
java.sql.CallableStatement
interfaces. Furthermore,
Connection
de-
clares various
createStatement()
,
prepareStatement
, and
pre-
pareCall()
methods that return
Statement
,
PreparedStatement
, or
CallableStatement
implementation instances, respectively.
Statement and ResultSet
Statement
istheeasiest-to-useinterface,and
Connection
's
Statement cre-
ateStatement()
methodistheeasiest-to-usemethodforobtaininga
Statement
instance.Aftercallingthismethod,youcanexecutevariousSQLstatementsbyinvok-
ing
Statement
methods such as the following:
•
ResultSet executeQuery(String sql)
executes a
SELECT
state-
mentand(assumingnoexceptionisthrown)providesaccesstoitsresultsviaa
java.sql.ResultSet
instance.
•
int executeUpdate(String sql)
executes a
CREATE TABLE
,
INSERT
,
UPDATE
,
DELETE
, or
DROP TABLE
statement and (assuming no
exceptionisthrown)typicallyreturnsthenumberoftablerowsaffectedbythis
statement.
9-17
presents its source code.
Listing 9-17.
Creating, inserting values into, querying, and dropping an
EMPLOYEES
table
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;