Interface Connection
- All Superinterfaces:
AutoCloseable,Wrapper
- All Known Implementing Classes:
JDBCConnection
public interface Connection extends Wrapper, AutoCloseable
- 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 SQLWHEREclause
-
Field Summary
Fields Modifier and Type Field Description static intTRANSACTION_NONEA constant indicating that transactions are not supported.static intTRANSACTION_READ_COMMITTEDNo 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.static intTRANSACTION_READ_UNCOMMITTEDIn 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 readsstatic intTRANSACTION_REPEATABLE_READA constant indicating that dirty reads and non-repeatable reads are prevented but phantom reads can occur.static intTRANSACTION_SERIALIZABLEThe constant that indicates that the following incidents are all prevented (the opposite ofTRANSACTION_READ_UNCOMMITTED): dirty reads non-repeatable reads phantom reads -
Method Summary
Modifier and Type Method Description voidclearWarnings()Discards all warnings that may have arisen for this connection.voidclose()Causes the instant release of all database and driver connection resources associated with this object.voidcommit()Commits all of the changes made since the lastcommitorrollbackof the associated transaction.ArraycreateArrayOf(String typeName, Object[] elements)Returns a newArraycontaining the givenelements.BlobcreateBlob()Returns a new empty Blob.ClobcreateClob()Returns a new empty Clob.NClobcreateNClob()Returns a new empty NClob.SQLXMLcreateSQLXML()Returns a new empty SQLXML.StatementcreateStatement()Returns a new instance ofStatementfor issuing SQL commands to the remote database.StatementcreateStatement(int resultSetType, int resultSetConcurrency)Returns a new instance ofStatementwhose associatedResultSets have the characteristics specified in the type and concurrency arguments.StatementcreateStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)Returns a new instance ofStatementwhose associatedResultSets will have the characteristics specified in the type, concurrency and holdability arguments.StructcreateStruct(String typeName, Object[] attributes)Returns a newStructcontaining the givenattributes.booleangetAutoCommit()Returns abooleanindicating whether or not this connection is in theauto-commitoperating mode.StringgetCatalog()Gets thisConnectionobject's current catalog name.PropertiesgetClientInfo()Returns aPropertiesobject containing all client info properties.StringgetClientInfo(String name)Returns the value corresponding to the given client info property, or null if unset.intgetHoldability()Returns the holdability property that anyResultSetproduced by this instance will have.DatabaseMetaDatagetMetaData()Gets the metadata about the database referenced by this connection.intgetTransactionIsolation()Returns the transaction isolation level for this connection.Map<String,Class<?>>getTypeMap()Returns the type mapping associated with thisConnectionobject.SQLWarninggetWarnings()Gets the first instance of anySQLWarningobjects that may have been created in the use of this connection.booleanisClosed()Returns abooleanindicating whether or not this connection is in theclosedstate.booleanisReadOnly()Returns abooleanindicating whether or not this connection is currently in theread-onlystate.booleanisValid(int timeout)Returns true if this connection is still open and valid, false otherwise.StringnativeSQL(String sql)Returns a string representation of the input SQL statementsqlexpressed in the underlying system's native SQL syntax.CallableStatementprepareCall(String sql)Returns a new instance ofCallableStatementthat may be used for making stored procedure calls to the database.CallableStatementprepareCall(String sql, int resultSetType, int resultSetConcurrency)Returns a new instance ofCallableStatementthat may be used for making stored procedure calls to the database.CallableStatementprepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)Returns a new instance ofCallableStatementthat may be used for making stored procedure calls to the database.PreparedStatementprepareStatement(String sql)Returns a new instance ofPreparedStatementthat may be used any number of times to execute parameterized requests on the database server.PreparedStatementprepareStatement(String sql, int autoGeneratedKeys)Creates a defaultPreparedStatementthat can retrieve automatically generated keys.PreparedStatementprepareStatement(String sql, int[] columnIndexes)Creates a defaultPreparedStatementthat can retrieve the auto-generated keys designated by a supplied array.PreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency)Creates aPreparedStatementthat generatesResultSets with the specified values ofresultSetTypeandresultSetConcurrency.PreparedStatementprepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)Creates aPreparedStatementthat generatesResultSets with the specified type, concurrency and holdabilityPreparedStatementprepareStatement(String sql, String[] columnNames)Creates a defaultPreparedStatementthat can retrieve the auto-generated keys designated by a supplied array.voidreleaseSavepoint(Savepoint savepoint)Releases the specifiedsavepointfrom the present transaction.voidrollback()Rolls back all updates made so far in this transaction and relinquishes all acquired database locks.voidrollback(Savepoint savepoint)Undoes all changes made after the suppliedSavepointobject was set.voidsetAutoCommit(boolean autoCommit)Sets this connection's auto-commit modeonoroff.voidsetCatalog(String catalog)Sets the catalog name for this connection.voidsetClientInfo(String name, String value)Sets the client info propertynametovalue.voidsetClientInfo(Properties properties)Replaces all client info properties with the name/value pairs fromproperties.voidsetHoldability(int holdability)Sets the holdability of theResultSets created by this Connection.voidsetReadOnly(boolean readOnly)Sets this connection to read-only mode.SavepointsetSavepoint()Creates an unnamedSavepointin the current transaction.SavepointsetSavepoint(String name)Creates a namedSavepointin the current transaction.voidsetTransactionIsolation(int level)Sets the transaction isolation level for this Connection.voidsetTypeMap(Map<String,Class<?>> map)Sets theTypeMapfor this connection.Methods inherited from interface java.sql.Wrapper
isWrapperFor, unwrap
-
Field Details
-
TRANSACTION_NONE
static final int TRANSACTION_NONEA constant indicating that transactions are not supported.- See Also:
- Constant Field Values
-
TRANSACTION_READ_COMMITTED
static final int TRANSACTION_READ_COMMITTEDNo 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.- See Also:
- Constant Field Values
-
TRANSACTION_READ_UNCOMMITTED
static final int TRANSACTION_READ_UNCOMMITTEDIn 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
- See Also:
- Constant Field Values
-
TRANSACTION_REPEATABLE_READ
static final int TRANSACTION_REPEATABLE_READA constant indicating that dirty reads and non-repeatable reads are prevented but phantom reads can occur.- See Also:
- Constant Field Values
-
TRANSACTION_SERIALIZABLE
static final int TRANSACTION_SERIALIZABLEThe constant that indicates that the following incidents are all prevented (the opposite ofTRANSACTION_READ_UNCOMMITTED):- dirty reads
- non-repeatable reads
- phantom reads
- See Also:
- Constant Field Values
-
-
Method Details
-
clearWarnings
Discards all warnings that may have arisen for this connection. Subsequent calls togetWarnings()will returnnullup until a new warning condition occurs.- Throws:
SQLException- if there is a problem accessing the database.
-
close
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
finalizemethod of the connection closes the connection before garbage collection takes place, it is not advisable to leave thecloseoperation to take place in this way. Mainly because undesired side-effects may appear.- Specified by:
closein interfaceAutoCloseable- Throws:
SQLException- if there is a problem accessing the database.
-
commit
Commits all of the changes made since the lastcommitorrollbackof the associated transaction. All locks in the database held by this connection are also relinquished. Calling this operation on connection objects inauto-commitmode 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.
-
createStatement
Returns a new instance ofStatementfor issuing SQL commands to the remote database.ResultSetsgenerated by the returned statement will default to typeResultSet.TYPE_FORWARD_ONLYand concurrency levelResultSet.CONCUR_READ_ONLY.- Returns:
- a
Statementobject with default settings. - Throws:
SQLException- if there is a problem accessing the database.- See Also:
ResultSet
-
createStatement
Returns a new instance ofStatementwhose associatedResultSets have the characteristics specified in the type and concurrency arguments.- Parameters:
resultSetType- one of the following type specifiers:resultSetConcurrency- one of the following concurrency mode specifiers:- Returns:
- a new instance of
Statementcapable of manufacturingResultSets that satisfy the specifiedresultSetTypeandresultSetConcurrencyvalues. - Throws:
SQLException- if there is a problem accessing the database
-
createStatement
Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLExceptionReturns a new instance ofStatementwhose associatedResultSets will have the characteristics specified in the type, concurrency and holdability arguments.- Parameters:
resultSetType- one of the following type specifiers:resultSetConcurrency- one of the following concurrency mode specifiers:resultSetHoldability- one of the following holdability mode specifiers:- Returns:
- a new instance of
Statementcapable of manufacturingResultSets that satisfy the specifiedresultSetType,resultSetConcurrencyandresultSetHoldabilityvalues. - Throws:
SQLException- if there is a problem accessing the database.
-
getAutoCommit
Returns abooleanindicating whether or not this connection is in theauto-commitoperating mode.- Returns:
trueifauto-commitis on, otherwisefalse.- Throws:
SQLException- if there is a problem accessing the database.
-
getCatalog
Gets thisConnectionobject's current catalog name.- Returns:
- the catalog name.
nullif there is no catalog name. - Throws:
SQLException- if there is a problem accessing the database.
-
getHoldability
Returns the holdability property that anyResultSetproduced by this instance will have.- Returns:
- one of the following holdability mode specifiers:
- Throws:
SQLException- if there is a problem accessing the a database.
-
getMetaData
Gets the metadata about the database referenced by this connection. The returnedDatabaseMetaDatadescribes the database topography, available stored procedures, SQL syntax and so on.- Returns:
- a
DatabaseMetaDataobject containing the database description. - Throws:
SQLException- if there is a problem accessing the a database.
-
getTransactionIsolation
Returns the transaction isolation level for this connection.- Returns:
- the transaction isolation value.
- Throws:
SQLException- if there is a problem accessing the database.- See Also:
TRANSACTION_NONE,TRANSACTION_READ_COMMITTED,TRANSACTION_READ_UNCOMMITTED,TRANSACTION_REPEATABLE_READ,TRANSACTION_SERIALIZABLE
-
getTypeMap
Returns the type mapping associated with thisConnectionobject. The type mapping must be set on the application level.- Returns:
- the Type Map as a
java.util.Map. - Throws:
SQLException- if there is a problem accessing the database.
-
getWarnings
Gets the first instance of anySQLWarningobjects 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. Anullindicates that no warnings have occurred.By invoking the
SQLWarning.getNextWarning()method of the returnedSQLWarningobject it is possible to obtain all of this connection's warning objects.- Returns:
- the first warning as an SQLWarning object (may be
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.
-
isClosed
Returns abooleanindicating whether or not this connection is in theclosedstate. Theclosedstate may be entered into as a consequence of a successful invocation of theclose()method or else if an error has occurred that prevents the connection from functioning normally.- Returns:
trueif closed, otherwisefalse.- Throws:
SQLException- if there is a problem accessing the database.
-
isReadOnly
Returns abooleanindicating whether or not this connection is currently in theread-onlystate.- Returns:
trueif in read-only state, otherwisefalse.- Throws:
SQLException- if there is a problem accessing the database.
-
nativeSQL
Returns a string representation of the input SQL statementsqlexpressed in the underlying system's native SQL syntax.- Parameters:
sql- the JDBC form of an SQL statement.- Returns:
- the SQL statement in native database format.
- Throws:
SQLException- if there is a problem accessing the database
-
prepareCall
Returns a new instance ofCallableStatementthat may be used for making stored procedure calls to the database.- Parameters:
sql- the SQL statement that calls the stored function- Returns:
- a new instance of
CallableStatementrepresenting the SQL statement.ResultSets emitted from thisCallableStatementwill default to typeResultSet.TYPE_FORWARD_ONLYand concurrencyResultSet.CONCUR_READ_ONLY. - Throws:
SQLException- if a problem occurs accessing the database.
-
prepareCall
CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLExceptionReturns a new instance ofCallableStatementthat may be used for making stored procedure calls to the database.ResultSets emitted from thisCallableStatementwill satisfy the specifiedresultSetTypeandresultSetConcurrencyvalues.- Parameters:
sql- the SQL statementresultSetType- one of the following type specifiers:resultSetConcurrency- one of the following concurrency mode specifiers:- Returns:
- a new instance of
CallableStatementrepresenting the precompiled SQL statement.ResultSets emitted from thisCallableStatementwill satisfy the specifiedresultSetTypeandresultSetConcurrencyvalues. - Throws:
SQLException- if a problem occurs accessing the database
-
prepareCall
CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLExceptionReturns a new instance ofCallableStatementthat may be used for making stored procedure calls to the database.ResultSets created from thisCallableStatementwill have characteristics determined by the specified type, concurrency and holdability arguments.- Parameters:
sql- the SQL statementresultSetType- one of the following type specifiers:resultSetConcurrency- one of the following concurrency mode specifiers:resultSetHoldability- one of the following holdability mode specifiers:- Returns:
- a new instance of
CallableStatementrepresenting the precompiled SQL statement.ResultSets emitted from thisCallableStatementwill satisfy the specifiedresultSetType,resultSetConcurrencyandresultSetHoldabilityvalues. - Throws:
SQLException- if a problem occurs accessing the database.
-
prepareStatement
Returns a new instance ofPreparedStatementthat 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
SQLExceptions get raised.By default,
ResultSets from the returned object will beResultSet.TYPE_FORWARD_ONLYtype with aResultSet.CONCUR_READ_ONLYmode of concurrency.- Parameters:
sql- the SQL statement.- Returns:
- the
PreparedStatementcontaining the supplied SQL statement. - Throws:
SQLException- if there is a problem accessing the database.
-
prepareStatement
Creates a defaultPreparedStatementthat can retrieve automatically generated keys. ParameterautoGeneratedKeysmay be used to tell the driver whether such keys should be made accessible. This is only relevant when thesqlstatement is aninsertstatement.An SQL statement which may have
INparameters can be stored and precompiled in aPreparedStatement. ThePreparedStatementcan 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
SQLExceptions get raised.By default,
ResultSets from the returned object will beResultSet.TYPE_FORWARD_ONLYtype with aResultSet.CONCUR_READ_ONLYmode of concurrency.- Parameters:
sql- the SQL statement.autoGeneratedKeys- one of the following generated key options:- Returns:
- a new
PreparedStatementinstance representing the input SQL statement. - Throws:
SQLException- if there is a problem accessing the database.
-
prepareStatement
Creates a defaultPreparedStatementthat can retrieve the auto-generated keys designated by a supplied array. Ifsqlis an SQLINSERTstatement, the parametercolumnIndexesis expected to hold the index values for each column in the statement's intended database table containing the autogenerated-keys of interest. OtherwisecolumnIndexesis 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
SQLExceptions get raised.By default,
ResultSets from the returned object will beResultSet.TYPE_FORWARD_ONLYtype with aResultSet.CONCUR_READ_ONLYconcurrency mode.- Parameters:
sql- the SQL statement.columnIndexes- the indexes of the columns for which auto-generated keys should be made available.- Returns:
- the PreparedStatement containing the supplied SQL statement.
- Throws:
SQLException- if a problem occurs accessing the database.
-
prepareStatement
PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLExceptionCreates aPreparedStatementthat generatesResultSets with the specified values ofresultSetTypeandresultSetConcurrency.- Parameters:
sql- the SQL statement. It can contain one or more'?'INparameter placeholders.resultSetType- one of the following type specifiers:resultSetConcurrency- one of the following concurrency mode specifiers:- Returns:
- a new instance of
PreparedStatementcontaining the SQL statementsql.ResultSets emitted from thisPreparedStatementwill satisfy the specifiedresultSetTypeandresultSetConcurrencyvalues. - Throws:
SQLException- if a problem occurs accessing the database.
-
prepareStatement
PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLExceptionCreates aPreparedStatementthat generatesResultSets with the specified type, concurrency and holdability- Parameters:
sql- the SQL statement. It can contain one or more'?' INparameter placeholders.resultSetType- one of the following type specifiers:resultSetConcurrency- one of the following concurrency mode specifiers:resultSetHoldability- one of the following holdability mode specifiers:- Returns:
- a new instance of
PreparedStatementcontaining the SQL statementsql.ResultSets emitted from thisPreparedStatementwill satisfy the specifiedresultSetType,resultSetConcurrencyandresultSetHoldabilityvalues. - Throws:
SQLException- if a problem occurs accessing the database.
-
prepareStatement
Creates a defaultPreparedStatementthat can retrieve the auto-generated keys designated by a supplied array. Ifsqlis an SQLINSERTstatement,columnNamesis expected to hold the names of each column in the statement's associated database table containing the autogenerated-keys of interest. OtherwisecolumnNamesis 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
SQLExceptioninstances get raised.By default, ResultSets from the returned object will be
ResultSet.TYPE_FORWARD_ONLYtype with aResultSet.CONCUR_READ_ONLYconcurrency mode.- Parameters:
sql- the SQL statement.columnNames- the names of the columns for which auto-generated keys should be made available.- Returns:
- the PreparedStatement containing the supplied SQL statement.
- Throws:
SQLException- if a problem occurs accessing the database.
-
releaseSavepoint
Releases the specifiedsavepointfrom the present transaction. Once removed, theSavepointis considered invalid and should not be referenced further.- Parameters:
savepoint- the object targeted for removal.- Throws:
SQLException- if there is a problem with accessing the database or ifsavepointis considered not valid in this transaction.
-
rollback
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.
-
rollback
Undoes all changes made after the suppliedSavepointobject was set. This method should only be used when auto-commit mode is disabled.- Parameters:
savepoint- the Savepoint to roll back to- Throws:
SQLException- if there is a problem accessing the database.
-
setAutoCommit
Sets this connection's auto-commit modeonoroff.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
offmeans that associated SQL statements get grouped into transactions that need to be completed by explicit calls to either thecommit()orrollback()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
ResultSets and output parameters have been obtained or closed.Calling this operation during an uncommitted transaction will result in it being committed.
- Parameters:
autoCommit-booleanindication of whether to put the target connection into auto-commit mode (true) or not (false).- Throws:
SQLException- if there is a problem accessing the database.
-
setCatalog
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.- Parameters:
catalog- the catalog name to use.- Throws:
SQLException- if there is a problem accessing the database.
-
setHoldability
Sets the holdability of theResultSets created by this Connection.- Parameters:
holdability- one of the following holdability mode specifiers:- Throws:
SQLException- if there is a problem accessing the database
-
setReadOnly
Sets this connection to read-only mode.This serves as a hint to the driver, which can enable database optimizations.
- Parameters:
readOnly-trueto set the Connection to read only mode.falsedisables read-only mode.- Throws:
SQLException- if there is a problem accessing the database.
-
setSavepoint
Creates an unnamedSavepointin the current transaction.- Returns:
- a
Savepointobject for this savepoint. - Throws:
SQLException- if there is a problem accessing the database.
-
setSavepoint
Creates a namedSavepointin the current transaction.- Parameters:
name- the name to use for the newSavepoint.- Returns:
- a
Savepointobject for this savepoint. - Throws:
SQLException- if there is a problem accessing the database.
-
setTransactionIsolation
Sets the transaction isolation level for this Connection.If this method is called during a transaction, the results are implementation defined.
- Parameters:
level- the new transaction isolation level to use from the following list of possible values:- Throws:
SQLException- if there is a problem with the database or if the value oflevelis not one of the expected constant values.
-
setTypeMap
Sets theTypeMapfor this connection. The inputmapshould contain mappings between complex Java and SQL types.- Parameters:
map- the new type map.- Throws:
SQLException- if there is a problem accessing the database or ifmapis not an instance ofMap.
-
createClob
Returns a new empty Clob.- Throws:
SQLException- if this connection is closed, or there's a problem creating a new clob.
-
createBlob
Returns a new empty Blob.- Throws:
SQLException- if this connection is closed, or there's a problem creating a new blob.
-
createNClob
Returns a new empty NClob.- Throws:
SQLException- if this connection is closed, or there's a problem creating a new nclob.
-
createSQLXML
Returns a new empty SQLXML.- Throws:
SQLException- if this connection is closed, or there's a problem creating a new XML.
-
isValid
Returns true if this connection is still open and valid, false otherwise.- Parameters:
timeout- number of seconds to wait for a response before giving up and returning false, 0 to wait forever- Throws:
SQLException- iftimeout < 0
-
setClientInfo
Sets the client info propertynametovalue. A value of null clears the client info property.- Throws:
SQLClientInfoException- if this connection is closed, or there's a problem setting the property.
-
setClientInfo
Replaces all client info properties with the name/value pairs fromproperties. All existing properties are removed. If an exception is thrown, the resulting state of this connection's client info properties is undefined.- Throws:
SQLClientInfoException- if this connection is closed, or there's a problem setting a property.
-
getClientInfo
Returns the value corresponding to the given client info property, or null if unset.- Throws:
SQLClientInfoException- if this connection is closed, or there's a problem getting the property.SQLException
-
getClientInfo
Returns aPropertiesobject containing all client info properties.- Throws:
SQLClientInfoException- if this connection is closed, or there's a problem getting a property.SQLException
-
createArrayOf
Returns a newArraycontaining the givenelements.- Parameters:
typeName- the SQL name of the type of the array elements- Throws:
SQLClientInfoException- if this connection is closed, or there's a problem creating the array.SQLException
-
createStruct
Returns a newStructcontaining the givenattributes.- Parameters:
typeName- the SQL name of the type of the struct attributes- Throws:
SQLClientInfoException- if this connection is closed, or there's a problem creating the array.SQLException
-