/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package java.sql; import java.util.Map; import java.util.Properties; /** * A connection represents a link from a Java application to a database. All SQL * statements and results are returned within the context of a connection. * Database statements that are executed within this context form a * database session which forms one or more closed transactions. Especially in * distributed applications, multiple concurrent connections may exist accessing * the same values of the database. which may lead to the following phenomena * (referred to as transaction isolation levels): *
* It is strongly recommended that all connections are closed before they * are dereferenced by the application ready for garbage collection. * Although the {@code finalize} method of the connection closes the * connection before garbage collection takes place, it is not advisable to * leave the {@code close} operation to take place in this way. Mainly * because undesired side-effects may appear. * * @throws SQLException * if there is a problem accessing the database. */ public void close() throws SQLException; /** * Commits all of the changes made since the last {@code commit} or * {@code rollback} of the associated transaction. All locks in the database * held by this connection are also relinquished. Calling this operation on * connection objects in {@code auto-commit} mode leads to an error. * * @throws SQLException * if there is a problem accessing the database or if the target * connection instance is in auto-commit mode. */ public void commit() throws SQLException; /** * Returns a new instance of {@code Statement} for issuing SQL commands to * the remote database. *
* {@code ResultSets} generated by the returned statement will default to * type {@code ResultSet.TYPE_FORWARD_ONLY} and concurrency level {@code * ResultSet.CONCUR_READ_ONLY}. * * @return a {@code Statement} object with default settings. * @throws SQLException * if there is a problem accessing the database. * @see ResultSet */ public Statement createStatement() throws SQLException; /** * Returns a new instance of {@code Statement} whose associated {@code * ResultSet}s have the characteristics specified in the type and * concurrency arguments. * * @param resultSetType * one of the following type specifiers: *
* By invoking the {@link SQLWarning#getNextWarning()} method of the * returned {@code SQLWarning} object it is possible to obtain all of * this connection's warning objects. * * @return the first warning as an SQLWarning object (may be {@code null}). * @throws SQLException * if there is a problem accessing the database or if the call * has been made on a connection which has been previously * closed. */ public SQLWarning getWarnings() throws SQLException; /** * Returns a {@code boolean} indicating whether or not this connection is in * the {@code closed} state. The {@code closed} state may be entered into as * a consequence of a successful invocation of the {@link #close()} method * or else if an error has occurred that prevents the connection from * functioning normally. * * @return {@code true} if closed, otherwise {@code false}. * @throws SQLException * if there is a problem accessing the database. */ public boolean isClosed() throws SQLException; /** * Returns a {@code boolean} indicating whether or not this connection is * currently in the {@code read-only} state. * * @return {@code true} if in read-only state, otherwise {@code false}. * @throws SQLException * if there is a problem accessing the database. */ public boolean isReadOnly() throws SQLException; /** * Returns a string representation of the input SQL statement * {@code sql} expressed in the underlying system's native SQL * syntax. * * @param sql * the JDBC form of an SQL statement. * @return the SQL statement in native database format. * @throws SQLException * if there is a problem accessing the database */ public String nativeSQL(String sql) throws SQLException; /** * Returns a new instance of {@code CallableStatement} that may be used for * making stored procedure calls to the database. * * @param sql * the SQL statement that calls the stored function * @return a new instance of {@code CallableStatement} representing the SQL * statement. {@code ResultSet}s emitted from this {@code * CallableStatement} will default to type * {@link ResultSet#TYPE_FORWARD_ONLY} and concurrency * {@link ResultSet#CONCUR_READ_ONLY}. * @throws SQLException * if a problem occurs accessing the database. */ public CallableStatement prepareCall(String sql) throws SQLException; /** * Returns a new instance of {@code CallableStatement} that may be used for * making stored procedure calls to the database. {@code ResultSet}s emitted * from this {@code CallableStatement} will satisfy the specified {@code * resultSetType} and {@code resultSetConcurrency} values. * * @param sql * the SQL statement * @param resultSetType * one of the following type specifiers: *
* Subject to JDBC driver support, this operation will attempt to send the * precompiled version of the statement to the database. If * the driver does not support precompiled statements, the statement will * not reach the database server until it is executed. This distinction * determines the moment when {@code SQLException}s get raised. *
* By default, {@code ResultSet}s from the returned object will be * {@link ResultSet#TYPE_FORWARD_ONLY} type with a * {@link ResultSet#CONCUR_READ_ONLY} mode of concurrency. * * @param sql * the SQL statement. * @return the {@code PreparedStatement} containing the supplied SQL * statement. * @throws SQLException * if there is a problem accessing the database. */ public PreparedStatement prepareStatement(String sql) throws SQLException; /** * Creates a default {@code PreparedStatement} that can retrieve * automatically generated keys. Parameter {@code autoGeneratedKeys} may be * used to tell the driver whether such keys should be made accessible. * This is only relevant when the {@code sql} statement is an {@code insert} * statement. *
* An SQL statement which may have {@code IN} parameters can be stored and * precompiled in a {@code PreparedStatement}. The {@code PreparedStatement} * can then be then be used to execute the statement multiple times in an * efficient way. *
* Subject to JDBC driver support, this operation will attempt to send the * precompiled version of the statement to the database. If * the driver does not support precompiled statements, the statement will * not reach the database server until it is executed. This distinction * determines the moment when {@code SQLException}s get raised. *
* By default, {@code ResultSet}s from the returned object will be * {@link ResultSet#TYPE_FORWARD_ONLY} type with a * {@link ResultSet#CONCUR_READ_ONLY} mode of concurrency. * * @param sql * the SQL statement. * @param autoGeneratedKeys * one of the following generated key options: *
* Subject to JDBC driver support, this operation will attempt to send the * precompiled version of the statement to the database. If * the driver does not support precompiled statements, the statement will * not reach the database server until it is executed. This distinction * determines the moment when {@code SQLException}s get raised. *
* By default, {@code ResultSet}s from the returned object will be * {@link ResultSet#TYPE_FORWARD_ONLY} type with a * {@link ResultSet#CONCUR_READ_ONLY} concurrency mode. * * @param sql * the SQL statement. * @param columnIndexes * the indexes of the columns for which auto-generated keys * should be made available. * @return the PreparedStatement containing the supplied SQL statement. * @throws SQLException * if a problem occurs accessing the database. */ public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException; /** * Creates a {@code PreparedStatement} that generates {@code ResultSet}s * with the specified values of {@code resultSetType} and {@code * resultSetConcurrency}. * * @param sql * the SQL statement. It can contain one or more {@code '?'} * {@code IN} parameter placeholders. * @param resultSetType * one of the following type specifiers: *
* Subject to JDBC driver support, this operation will attempt to send the * precompiled version of the statement to the database. Alternatively, if * the driver is not capable of handling precompiled statements, the * statement will not reach the database server until it is executed. This * will have a bearing on precisely when {@code SQLException} * instances get raised. *
* By default, ResultSets from the returned object will be * {@link ResultSet#TYPE_FORWARD_ONLY} type with a * {@link ResultSet#CONCUR_READ_ONLY} concurrency mode. * * @param sql * the SQL statement. * @param columnNames * the names of the columns for which auto-generated keys should * be made available. * @return the PreparedStatement containing the supplied SQL statement. * @throws SQLException * if a problem occurs accessing the database. */ public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException; /** * Releases the specified {@code savepoint} from the present transaction. Once removed, * the {@code Savepoint} is considered invalid and should not be referenced * further. * * @param savepoint * the object targeted for removal. * @throws SQLException * if there is a problem with accessing the database or if * {@code savepoint} is considered not valid in this * transaction. */ public void releaseSavepoint(Savepoint savepoint) throws SQLException; /** * Rolls back all updates made so far in this transaction and * relinquishes all acquired database locks. It is an error to invoke this * operation when in auto-commit mode. * * @throws SQLException * if there is a problem with the database or if the method is * called while in auto-commit mode of operation. */ public void rollback() throws SQLException; /** * Undoes all changes made after the supplied {@code Savepoint} object was * set. This method should only be used when auto-commit mode is disabled. * * @param savepoint * the Savepoint to roll back to * @throws SQLException * if there is a problem accessing the database. */ public void rollback(Savepoint savepoint) throws SQLException; /** * Sets this connection's auto-commit mode {@code on} or {@code off}. *
* Putting a Connection into auto-commit mode means that all associated SQL * statements are run and committed as separate transactions. * By contrast, setting auto-commit to {@code off} means that associated SQL * statements get grouped into transactions that need to be completed by * explicit calls to either the {@link #commit()} or {@link #rollback()} * methods. *
* Auto-commit is the default mode for new connection instances. *
* When in this mode, commits will automatically occur upon successful SQL * statement completion or upon successful completion of an execute. * Statements are not considered successfully completed until all associated * {@code ResultSet}s and output parameters have been obtained or closed. *
* Calling this operation during an uncommitted transaction will result in * it being committed. * * @param autoCommit * {@code boolean} indication of whether to put the target * connection into auto-commit mode ({@code true}) or not ( * {@code false}). * @throws SQLException * if there is a problem accessing the database. */ public void setAutoCommit(boolean autoCommit) throws SQLException; /** * Sets the catalog name for this connection. This is used to select a * subspace of the database for future work. If the driver does not support * catalog names, this method is ignored. * * @param catalog * the catalog name to use. * @throws SQLException * if there is a problem accessing the database. */ public void setCatalog(String catalog) throws SQLException; /** * Sets the holdability of the {@code ResultSet}s created by this Connection. * * @param holdability * one of the following holdability mode specifiers: *
* This serves as a hint to the driver, which can enable database * optimizations. * * @param readOnly * {@code true} to set the Connection to read only mode. {@code * false} disables read-only mode. * @throws SQLException * if there is a problem accessing the database. */ public void setReadOnly(boolean readOnly) throws SQLException; /** * Creates an unnamed {@code Savepoint} in the current transaction. * * @return a {@code Savepoint} object for this savepoint. * @throws SQLException * if there is a problem accessing the database. */ public Savepoint setSavepoint() throws SQLException; /** * Creates a named {@code Savepoint} in the current transaction. * * @param name * the name to use for the new {@code Savepoint}. * @return a {@code Savepoint} object for this savepoint. * @throws SQLException * if there is a problem accessing the database. */ public Savepoint setSavepoint(String name) throws SQLException; /** * Sets the transaction isolation level for this Connection. *
* If this method is called during a transaction, the results are * implementation defined. * * @param level * the new transaction isolation level to use from the following * list of possible values: *