FileDocCategorySizeDatePackage
Connection.javaAPI DocAndroid 1.5 API34144Wed May 06 22:41:06 BST 2009java.sql

Connection

public interface Connection
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):
  • dirty reads:
    reading values from table rows that are not committed.
  • non-repeatable reads:
    reading table rows more than once in a transaction but getting back different data because other transactions have altered the rows between the reads.
  • phantom reads:
    retrieving additional "phantom" rows in the course of repeated table reads because other transactions have inserted additional rows that satisfy an SQL {@code WHERE} clause
since
Android 1.0

Fields Summary
public static final int
TRANSACTION_NONE
A constant indicating that transactions are not supported.
public static final int
TRANSACTION_READ_COMMITTED
No dirty reads are permitted, therefore transactions may not read a row containing uncommitted values - but does not prevent an application from non-repeatable reads and phantom reads.
public static final int
TRANSACTION_READ_UNCOMMITTED
In the case that reading uncommitted values is allowed, the following incidents may happen which may lead to an invalid results:
  • dirty reads
  • non-repeatable reads
  • phantom reads
public static final int
TRANSACTION_REPEATABLE_READ
A constant indicating that dirty reads and non-repeatable reads are prevented but phantom reads can occur.
public static final int
TRANSACTION_SERIALIZABLE
The constant that indicates that the following incidents are all prevented (the opposite of {@link #TRANSACTION_READ_UNCOMMITTED}):
  • dirty reads
  • non-repeatable reads
  • phantom reads
Constructors Summary
Methods Summary
public voidclearWarnings()
Discards all warnings that may have arisen for this connection. Subsequent calls to {@link #getWarnings()} will return {@code null} up until a new warning condition occurs.

throws
SQLException if there is a problem accessing the database.

public voidclose()
Causes the instant release of all database and driver connection resources associated with this object. Any subsequent invocations of this method have no effect.

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 voidcommit()
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 java.sql.StatementcreateStatement()
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 java.sql.StatementcreateStatement(int resultSetType, int resultSetConcurrency)
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:
  • {@link ResultSet#TYPE_SCROLL_SENSITIVE}
  • {@link ResultSet#TYPE_SCROLL_INSENSITIVE}
  • {@link ResultSet#TYPE_FORWARD_ONLY}
param
resultSetConcurrency one of the following concurrency mode specifiers:
  • {@link ResultSet#CONCUR_UPDATABLE}
  • {@link ResultSet#CONCUR_READ_ONLY}
return
a new instance of {@code Statement} capable of manufacturing {@code ResultSet}s that satisfy the specified {@code resultSetType} and {@code resultSetConcurrency} values.
throws
SQLException if there is a problem accessing the database

public java.sql.StatementcreateStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Returns a new instance of {@code Statement} whose associated {@code ResultSet}s will have the characteristics specified in the type, concurrency and holdability arguments.

param
resultSetType one of the following type specifiers:
  • {@link ResultSet#TYPE_SCROLL_SENSITIVE}
  • {@link ResultSet#TYPE_SCROLL_INSENSITIVE}
  • {@link ResultSet#TYPE_FORWARD_ONLY}
param
resultSetConcurrency one of the following concurrency mode specifiers:
  • {@link ResultSet#CONCUR_UPDATABLE}
  • {@link ResultSet#CONCUR_READ_ONLY}
param
resultSetHoldability one of the following holdability mode specifiers:
  • {@link ResultSet#HOLD_CURSORS_OVER_COMMIT}
  • {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}
return
a new instance of {@code Statement} capable of manufacturing {@code ResultSet}s that satisfy the specified {@code resultSetType}, {@code resultSetConcurrency} and {@code resultSetHoldability} values.
throws
SQLException if there is a problem accessing the database.

public booleangetAutoCommit()
Returns a {@code boolean} indicating whether or not this connection is in the {@code auto-commit} operating mode.

return
{@code true} if {@code auto-commit} is on, otherwise {@code false}.
throws
SQLException if there is a problem accessing the database.
since
Android 1.0

public java.lang.StringgetCatalog()
Gets this {@code Connection} object's current catalog name.

return
the catalog name. {@code null} if there is no catalog name.
throws
SQLException if there is a problem accessing the database.

public intgetHoldability()
Returns the holdability property that any {@code ResultSet} produced by this instance will have.

return
one of the following holdability mode specifiers:
  • {@link ResultSet#HOLD_CURSORS_OVER_COMMIT}
  • {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}
throws
SQLException if there is a problem accessing the a database.
since
Android 1.0

public java.sql.DatabaseMetaDatagetMetaData()
Gets the metadata about the database referenced by this connection. The returned {@code DatabaseMetaData} describes the database topography, available stored procedures, SQL syntax and so on.

return
a {@code DatabaseMetaData} object containing the database description.
throws
SQLException if there is a problem accessing the a database.
since
Android 1.0

public intgetTransactionIsolation()
Returns the transaction isolation level for this connection.

return
the transaction isolation value.
throws
SQLException if there is a problem accessing the database.
see
#TRANSACTION_NONE
see
#TRANSACTION_READ_COMMITTED
see
#TRANSACTION_READ_UNCOMMITTED
see
#TRANSACTION_REPEATABLE_READ
see
#TRANSACTION_SERIALIZABLE
since
Android 1.0

public java.util.MapgetTypeMap()
Returns the type mapping associated with this {@code Connection} object. The type mapping must be set on the application level.

return
the Type Map as a {@code java.util.Map}.
throws
SQLException if there is a problem accessing the database.
since
Android 1.0

public java.sql.SQLWarninggetWarnings()
Gets the first instance of any {@code SQLWarning} objects that may have been created in the use of this connection. If at least one warning has occurred then this operation returns the first one reported. A {@code null} indicates that no warnings have occurred.

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.
since
Android 1.0

public booleanisClosed()
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.
since
Android 1.0

public booleanisReadOnly()
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.
since
Android 1.0

public java.lang.StringnativeSQL(java.lang.String sql)
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 java.sql.CallableStatementprepareCall(java.lang.String sql)
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.
since
Android 1.0

public java.sql.CallableStatementprepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency)
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:
  • {@link ResultSet#TYPE_SCROLL_SENSITIVE}
  • {@link ResultSet#TYPE_SCROLL_INSENSITIVE}
  • {@link ResultSet#TYPE_FORWARD_ONLY}
param
resultSetConcurrency one of the following concurrency mode specifiers:
  • {@link ResultSet#CONCUR_READ_ONLY}
  • {@link ResultSet#CONCUR_UPDATABLE}
return
a new instance of {@code CallableStatement} representing the precompiled SQL statement. {@code ResultSet}s emitted from this {@code CallableStatement} will satisfy the specified {@code resultSetType} and {@code resultSetConcurrency} values.
throws
SQLException if a problem occurs accessing the database
since
Android 1.0

public java.sql.CallableStatementprepareCall(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Returns a new instance of {@code CallableStatement} that may be used for making stored procedure calls to the database. {@code ResultSet}s created from this {@code CallableStatement} will have characteristics determined by the specified type, concurrency and holdability arguments.

param
sql the SQL statement
param
resultSetType one of the following type specifiers:
  • {@link ResultSet#TYPE_SCROLL_SENSITIVE}
  • {@link ResultSet#TYPE_SCROLL_INSENSITIVE}
  • {@link ResultSet#TYPE_FORWARD_ONLY}
param
resultSetConcurrency one of the following concurrency mode specifiers:
  • {@link ResultSet#CONCUR_READ_ONLY}
  • {@link ResultSet#CONCUR_UPDATABLE}
param
resultSetHoldability one of the following holdability mode specifiers:
  • {@link ResultSet#HOLD_CURSORS_OVER_COMMIT}
  • {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}
return
a new instance of {@code CallableStatement} representing the precompiled SQL statement. {@code ResultSet}s emitted from this {@code CallableStatement} will satisfy the specified {@code resultSetType}, {@code resultSetConcurrency} and {@code resultSetHoldability} values.
throws
SQLException if a problem occurs accessing the database.
since
Android 1.0

public java.sql.PreparedStatementprepareStatement(java.lang.String sql)
Returns a new instance of {@code PreparedStatement} that may be used any number of times to execute parameterized requests on the database server.

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.
since
Android 1.0

public java.sql.PreparedStatementprepareStatement(java.lang.String sql, int autoGeneratedKeys)
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:
  • {@link Statement#RETURN_GENERATED_KEYS}
  • {@link Statement#NO_GENERATED_KEYS}
return
a new {@code PreparedStatement} instance representing the input SQL statement.
throws
SQLException if there is a problem accessing the database.
since
Android 1.0

public java.sql.PreparedStatementprepareStatement(java.lang.String sql, int[] columnIndexes)
Creates a default {@code PreparedStatement} that can retrieve the auto-generated keys designated by a supplied array. If {@code sql} is an SQL {@code INSERT} statement, the parameter {@code columnIndexes} is expected to hold the index values for each column in the statement's intended database table containing the autogenerated-keys of interest. Otherwise {@code columnIndexes} is ignored.

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.
since
Android 1.0

public java.sql.PreparedStatementprepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency)
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:
  • {@link ResultSet#TYPE_SCROLL_SENSITIVE}
  • {@link ResultSet#TYPE_SCROLL_INSENSITIVE}
  • {@link ResultSet#TYPE_FORWARD_ONLY}
param
resultSetConcurrency one of the following concurrency mode specifiers:
  • {@link ResultSet#CONCUR_READ_ONLY}
  • {@link ResultSet#CONCUR_UPDATABLE}
return
a new instance of {@code PreparedStatement} containing the SQL statement {@code sql}. {@code ResultSet}s emitted from this {@code PreparedStatement} will satisfy the specified {@code resultSetType} and {@code resultSetConcurrency} values.
throws
SQLException if a problem occurs accessing the database.
since
Android 1.0

public java.sql.PreparedStatementprepareStatement(java.lang.String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
Creates a {@code PreparedStatement} that generates {@code ResultSet}s with the specified type, concurrency and holdability

param
sql the SQL statement. It can contain one or more {@code '?' IN} parameter placeholders.
param
resultSetType one of the following type specifiers:
  • {@link ResultSet#TYPE_SCROLL_SENSITIVE}
  • {@link ResultSet#TYPE_SCROLL_INSENSITIVE}
  • {@link ResultSet#TYPE_FORWARD_ONLY}
param
resultSetConcurrency one of the following concurrency mode specifiers:
  • {@link ResultSet#CONCUR_READ_ONLY}
  • {@link ResultSet#CONCUR_UPDATABLE}
param
resultSetHoldability one of the following holdability mode specifiers:
  • {@link ResultSet#HOLD_CURSORS_OVER_COMMIT}
  • {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}
return
a new instance of {@code PreparedStatement} containing the SQL statement {@code sql}. {@code ResultSet}s emitted from this {@code PreparedStatement} will satisfy the specified {@code resultSetType}, {@code resultSetConcurrency} and {@code resultSetHoldability} values.
throws
SQLException if a problem occurs accessing the database.
since
Android 1.0

public java.sql.PreparedStatementprepareStatement(java.lang.String sql, java.lang.String[] columnNames)
Creates a default {@code PreparedStatement} that can retrieve the auto-generated keys designated by a supplied array. If {@code sql} is an SQL {@code INSERT} statement, {@code columnNames} is expected to hold the names of each column in the statement's associated database table containing the autogenerated-keys of interest. Otherwise {@code columnNames} is ignored.

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.
since
Android 1.0

public voidreleaseSavepoint(java.sql.Savepoint savepoint)
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.
since
Android 1.0

public voidrollback()
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.
since
Android 1.0

public voidrollback(java.sql.Savepoint savepoint)
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.
since
Android 1.0

public voidsetAutoCommit(boolean autoCommit)
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.
since
Android 1.0

public voidsetCatalog(java.lang.String catalog)
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.
since
Android 1.0

public voidsetHoldability(int holdability)
Sets the holdability of the {@code ResultSet}s created by this Connection.

param
holdability one of the following holdability mode specifiers:
  • {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}
  • {@link ResultSet#HOLD_CURSORS_OVER_COMMIT}
throws
SQLException if there is a problem accessing the database

public voidsetReadOnly(boolean readOnly)
Sets this connection to read-only mode.

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.
since
Android 1.0

public java.sql.SavepointsetSavepoint()
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.
since
Android 1.0

public java.sql.SavepointsetSavepoint(java.lang.String name)
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.
since
Android 1.0

public voidsetTransactionIsolation(int level)
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:
  • {@link #TRANSACTION_READ_COMMITTED}
  • {@link #TRANSACTION_READ_UNCOMMITTED}
  • {@link #TRANSACTION_REPEATABLE_READ}
  • {@link #TRANSACTION_SERIALIZABLE}
throws
SQLException if there is a problem with the database or if the value of {@code level} is not one of the expected constant values.
since
Android 1.0

public voidsetTypeMap(java.util.Map map)
Sets the {@code TypeMap} for this connection. The input {@code map} should contain mappings between complex Java and SQL types.

param
map the new type map.
throws
SQLException if there is a problem accessing the database or if {@code map} is not an instance of {@link Map}.
since
Android 1.0