Java Reference
In-Depth Information
public Statement createStatement(int resultSetType, int concurrency, int
holdability) throws SQLException. Similar to the previous method,
except a result set holdability property is denoted. The possible values
for holdability are HOLD_CURSORS_OVER_COMMIT or CLOSE_CUR-
SORS_AT_COMMIT, static fields in the ResultSet interface. These are
also discussed in the next section. For example, the following statements
create a Statement object using the default result set properties:
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection connection =
DriverManager.getConnection(“jdbc:odbc:someDSN”);
Statement statement = connection.createStatement();
A Statement object can represent any SQL statement, and the Statement
interface contains methods for defining and executing the SQL state-
ment. Some of the methods in the Statement interface include:
public ResultSet executeQuery(String sql) throws SQLException. Exe-
cutes a SQL statement that returns a single result set. Use this method
for SELECT statements.
public int executeUpdate(String sql) throws SQLException. Executes
the given SQL statement. Use this method for executing UPDATE,
INSERT, or DELETE statements or other statements that do not return a
result set. The return value is a row count of the number of rows affected
by the SQL statement.
public boolean execute(String sql) throws SQLException. Executes the
given SQL statement. This method is useful if you do not know what
type of SQL statement is being executed. If the statement generates a
result set, it can be obtained using the getResultSet() method.
For example, the following code executes an INSERT statement:
statement.executeUpdate(“INSERT INTO Employees VALUES (101, 20.00,
'Rich', 'Raposa')”);
You can also use a Statement object to create a batch of SQL statements
and then execute the entire batch at once. This is accomplished using the
following methods of the Statement interface:
public void addBatch(String sql) throws SQLException.
Add the
given SQL statement to the current batch.
public int [ ] executeBatch() throws SQLException. Executes all of
the SQL statements in the batch. The array of return values
corresponds to the statements in the batch and typically
represents a row count for each batch statement executed
successfully.
Search WWH ::




Custom Search