Most visited

Recently visited

Added in API level 1

Connection

public interface Connection
implements Wrapper, AutoCloseable

java.sql.Connection


与特定数据库的连接(会话)。 执行SQL语句并在连接的上下文中返回结果。

一个Connection对象的数据库能够提供描述它的表的信息,它支持的SQL语法,它的存储过程,这个连接的能力等等。 该信息通过getMetaData方法获得。

注意:配置Connection ,JDBC应用程序应使用适当的Connection方法,如setAutoCommitsetTransactionIsolation 当存在可用的JDBC方法时,应用程序不应该直接调用SQL命令来更改连接的配置。 默认情况下, Connection对象处于自动提交模式,这意味着它在执行每个语句后自动提交更改。 如果禁用了自动提交模式,则必须显式调用方法commit以提交更改; 否则,数据库更改将不会保存。

使用JDBC 2.1核心API创建的新对象Connection具有与其关联的初始空白类型映射。 用户可以在此类型映射中为UDT输入自定义映射。 当使用方法ResultSet.getObject从数据源中检索UDT时, getObject方法将检查连接的类型映射以查看是否存在该UDT的条目。 如果是这样, getObject方法将把UDT映射到指定的类。 如果没有条目,则UDT将使用标准映射进行映射。

用户可以创建一个新的类型映射,它是一个java.util.Map对象,在其中创建一个条目,并将其传递给可执行自定义映射的java.sql方法。 在这种情况下,该方法将使用给定的类型映射,而不是与连接关联的映射。

例如,以下代码片段指定SQL类型ATHLETES将以Java编程语言映射到类Athletes 该代码片段检索Connection对象con的类型映射,将条目插入其中,然后将新条目的类型映射设置为连接的类型映射。

      java.util.Map map = con.getTypeMap();
      map.put("mySchemaName.ATHLETES", Class.forName("Athletes"));
      con.setTypeMap(map);
 

也可以看看:

Summary

Constants

int TRANSACTION_NONE

表示不支持事务的常量。

int TRANSACTION_READ_COMMITTED

表示可以防止脏读的常量。 不可重复读取和幻像读取可能发生。

int TRANSACTION_READ_UNCOMMITTED

表示可能发生脏读,不可重复读和幻读的常量。

int TRANSACTION_REPEATABLE_READ

一个常量,指示防止脏读和不可重复读取; 幻像读取可能会发生。

int TRANSACTION_SERIALIZABLE

一个常量表示可以防止脏读,不可重复读取和幻读。

Public methods

abstract void clearWarnings()

清除为此 Connection对象报告的所有警告。

abstract void close()

立即释放此 Connection对象的数据库和JDBC资源,而不是等待它们自动发布。

abstract void commit()

自上次提交/回滚之后进行所有更改,并释放当前由此 Connection对象持有的任何数据库锁定。

abstract Array createArrayOf(String typeName, Object[] elements)

用于创建Array对象的工厂方法。

abstract Blob createBlob()

构造一个实现 Blob接口的对象。

abstract Clob createClob()

构造一个实现 Clob接口的对象。

abstract NClob createNClob()

构造一个实现 NClob接口的对象。

abstract SQLXML createSQLXML()

构造一个实现 SQLXML接口的对象。

abstract Statement createStatement(int resultSetType, int resultSetConcurrency)

创建一个 Statement对象,该对象将生成具有给定类型和并发性的 ResultSet对象。

abstract Statement createStatement()

创建一个用于将SQL语句发送到数据库的 Statement对象。

abstract Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)

创建一个 Statement对象,该对象将生成具有给定类型,并发性和可保存性的 ResultSet对象。

abstract Struct createStruct(String typeName, Object[] attributes)

用于创建Struct对象的工厂方法。

abstract boolean getAutoCommit()

检索此 Connection对象的当前自动提交模式。

abstract String getCatalog()

获取此 Connection对象的当前目录名称。

abstract String getClientInfo(String name)

返回按名称指定的客户端信息属性的值。

abstract Properties getClientInfo()

返回包含驱动程序支持的每个客户端信息属性的名称和当前值的列表。

abstract int getHoldability()

检索使用此 Connection对象创建的 ResultSet对象的当前可保存性。

abstract DatabaseMetaData getMetaData()

检索 DatabaseMetaData对象包含有关哪个这个数据库的元数据 Connection对象表示的连接。

abstract int getTransactionIsolation()

检索此 Connection对象的当前事务隔离级别。

abstract Map<StringClass<?>> getTypeMap()

检索 Map与此相关联的对象 Connection对象。

abstract SQLWarning getWarnings()

检索此 Connection对象上由调用报告的第一个警告。

abstract boolean isClosed()

检索此 Connection对象是否已关闭。

abstract boolean isReadOnly()

检索此 Connection对象是否处于只读模式。

abstract boolean isValid(int timeout)

如果连接尚未关闭并且仍然有效,则返回true。

abstract String nativeSQL(String sql)

将给定的SQL语句转换为系统的原生SQL语法。

abstract CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)

创建一个 CallableStatement对象,该对象将生成具有给定类型和并发性的 ResultSet对象。

abstract CallableStatement prepareCall(String sql)

为调用数据库存储过程创建一个 CallableStatement对象。

abstract CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)

创建一个 CallableStatement对象,该对象将生成具有给定类型和并发性的 ResultSet对象。

abstract PreparedStatement prepareStatement(String sql, String[] columnNames)

创建一个默认的 PreparedStatement对象,该对象能够返回给定数组指定的自动生成的键。

abstract PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)

创建一个 PreparedStatement对象,该对象将生成具有给定类型,并发性和可保存性的 ResultSet对象。

abstract PreparedStatement prepareStatement(String sql, int[] columnIndexes)

创建一个默认的 PreparedStatement对象,该对象能够返回给定数组指定的自动生成的键。

abstract PreparedStatement prepareStatement(String sql)

创建一个 PreparedStatement对象,用于将参数化的SQL语句发送到数据库。

abstract PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)

创建一个能够检索自动生成的密钥的默认 PreparedStatement对象。

abstract PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)

创建一个 PreparedStatement对象,该对象将生成具有给定类型和并发性的 ResultSet对象。

abstract void releaseSavepoint(Savepoint savepoint)

从当前事务中移除指定的 Savepoint和随后的 Savepoint对象。

abstract void rollback()

取消当前事务中所做的所有更改,并释放当前由此 Connection对象持有的任何数据库锁定。

abstract void rollback(Savepoint savepoint)

取消给定的 Savepoint对象设置后所做的所有更改。

abstract void setAutoCommit(boolean autoCommit)

将此连接的自动提交模式设置为给定状态。

abstract void setCatalog(String catalog)

设置给定的目录名称,以便选择要在其中工作的此 Connection对象数据库的子空间。

abstract void setClientInfo(Properties properties)

设置连接的客户端信息属性的值。

abstract void setClientInfo(String name, String value)

将名称指定的客户端信息属性的值设置为值指定的值。

abstract void setHoldability(int holdability)

将使用此 Connection对象创建的 ResultSet对象的默认可保存性更改为给定的可保存性。

abstract void setReadOnly(boolean readOnly)

将此连接置于只读模式,作为驱动程序启动数据库优化的提示。

abstract Savepoint setSavepoint(String name)

在当前事务中使用给定名称创建一个保存点,并返回表示它的新对象 Savepoint

abstract Savepoint setSavepoint()

在当前事务中创建一个未命名的保存点,并返回表示它的新对象 Savepoint

abstract void setTransactionIsolation(int level)

尝试将此 Connection对象的事务隔离级别更改为给定的对象。

abstract void setTypeMap(Map<StringClass<?>> map)

安装给定 TypeMap对象的类型映射此 Connection对象。

Inherited methods

From interface java.sql.Wrapper
From interface java.lang.AutoCloseable

Constants

TRANSACTION_NONE

Added in API level 1
int TRANSACTION_NONE

表示不支持事务的常量。

常量值:0(0x00000000)

TRANSACTION_READ_COMMITTED

Added in API level 1
int TRANSACTION_READ_COMMITTED

表示可以防止脏读的常量。 不可重复读取和幻像读取可能发生。 该级别仅禁止事务读取其中未提交更改的行。

常量值:2(0x00000002)

TRANSACTION_READ_UNCOMMITTED

Added in API level 1
int TRANSACTION_READ_UNCOMMITTED

表示可能发生脏读,不可重复读和幻读的常量。 这个级别允许一个事务改变的行被另一个事务读取,然后该行的任何改变被提交(“脏读”)。 如果任何更改回滚,则第二个事务将检索到无效行。

常数值:1(0x00000001)

TRANSACTION_REPEATABLE_READ

Added in API level 1
int TRANSACTION_REPEATABLE_READ

一个常量,指示防止脏读和不可重复读取; 幻像读取可能会发生。 该级别禁止事务读取其中有未提交更改的行,并且还禁止一个事务读取一行,第二个事务更改该行并且第一个事务重新读取行,第二次获取不同值的情况一个“不可重复读”)。

常量值:4(0x00000004)

TRANSACTION_SERIALIZABLE

Added in API level 1
int TRANSACTION_SERIALIZABLE

一个常量表示可以防止脏读,不可重复读取和幻读。 此级别包括在禁止TRANSACTION_REPEATABLE_READ ,并进一步禁止在那里一个事务读取满足所有行的情况WHERE条件,第二个事务插入一行满足该WHERE条件,和第一个事务重新读取为相同的条件下,检索附加的“幻影“排在第二次阅读。

常量值:8(0x00000008)

Public methods

clearWarnings

Added in API level 1
void clearWarnings ()

清除为此Connection对象报告的所有警告。 在调用此方法后,方法getWarnings返回null直到为此Connection对象报告新警告为止。

Throws
SQLException SQLException if a database access error occurs or this method is called on a closed connection

close

Added in API level 1
void close ()

立即释放此 Connection对象的数据库和JDBC资源,而不是等待它们被自动释放。

在已关闭的 Connection对象上调用方法 close是无操作的。

强烈建议应用程序在调用close方法之前显式提交或回滚活动事务。 如果调用close方法并且存在活动事务,则结果是实现定义的。

Throws
SQLException SQLException if a database access error occurs

commit

Added in API level 1
void commit ()

自上次提交/回滚之后进行所有更改,并释放此Connection对象当前拥有的任何数据库锁定。 只有在禁用自动提交模式时才应使用此方法。

Throws
SQLException if a database access error occurs, this method is called while participating in a distributed transaction, if this method is called on a closed conection or this Connection object is in auto-commit mode

也可以看看:

createArrayOf

Added in API level 9
Array createArrayOf (String typeName, 
                Object[] elements)

用于创建Array对象的工厂方法。

注意:当使用 createArrayOf创建映射到基元数据类型的数组对象时, Array对象是否为该基元数据类型的数组或 Object的数组是实现定义的。

注意: JDBC驱动程序负责将元素Object数组映射到给定类Object java.sql.Types中定义的默认JDBC SQL类型。 默认映射在JDBC规范的附录B中指定。 如果生成的JDBC类型不是给定typeName的适当类型,那么实现将定义是否引发SQLException或驱动程序是否支持生成的转换。

Parameters
typeName String: the SQL name of the type the elements of the array map to. The typeName is a database-specific name which may be the name of a built-in type, a user-defined type or a standard SQL type supported by this database. This is the value returned by Array.getBaseTypeName
elements Object: the elements that populate the returned object
Returns
Array an Array object whose elements map to the specified SQL type
Throws
SQLException if a database error occurs, the JDBC type is not appropriate for the typeName and the conversion is not supported, the typeName is null or this method is called on a closed connection
SQLFeatureNotSupportedException if the JDBC driver does not support this data type

createBlob

Added in API level 9
Blob createBlob ()

构造一个实现Blob接口的对象。 返回的对象最初不包含数据。 Blob接口的setBinaryStreamsetBytes方法可用于将数据添加到Blob

Returns
Blob An object that implements the Blob interface
Throws
SQLException if an object that implements the Blob interface can not be constructed, this method is called on a closed connection or a database access error occurs.
SQLFeatureNotSupportedException if the JDBC driver does not support this data type

createClob

Added in API level 9
Clob createClob ()

构造一个实现Clob接口的对象。 返回的对象最初不包含数据。 setAsciiStreamsetCharacterStreamsetString所述的方法Clob接口可用于将数据添加到所述Clob

Returns
Clob An object that implements the Clob interface
Throws
SQLException if an object that implements the Clob interface can not be constructed, this method is called on a closed connection or a database access error occurs.
SQLFeatureNotSupportedException if the JDBC driver does not support this data type

createNClob

Added in API level 9
NClob createNClob ()

构造一个实现NClob接口的对象。 返回的对象最初不包含数据。 setAsciiStreamsetCharacterStreamsetString所述的方法NClob接口可用于将数据添加到所述NClob

Returns
NClob An object that implements the NClob interface
Throws
SQLException if an object that implements the NClob interface can not be constructed, this method is called on a closed connection or a database access error occurs.
SQLFeatureNotSupportedException if the JDBC driver does not support this data type

createSQLXML

Added in API level 9
SQLXML createSQLXML ()

构造一个实现SQLXML接口的对象。 返回的对象最初不包含数据。 SQLXML接口的createXmlStreamWriter对象和setString接口的方法可用于将数据添加到SQLXML对象。

Returns
SQLXML An object that implements the SQLXML interface
Throws
SQLException if an object that implements the SQLXML interface can not be constructed, this method is called on a closed connection or a database access error occurs.
SQLFeatureNotSupportedException if the JDBC driver does not support this data type

createStatement

Added in API level 1
Statement createStatement (int resultSetType, 
                int resultSetConcurrency)

创建一个Statement对象,该对象将生成具有给定类型和并发性的ResultSet对象。 此方法与createStatement方法createStatement相同,但它允许覆盖默认结果集类型和并发性。 创建的结果集的可保存性可通过调用getHoldability()来确定。

Parameters
resultSetType int: a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency int: a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
Returns
Statement a new Statement object that will generate ResultSet objects with the given type and concurrency
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameters are not ResultSet constants indicating type and concurrency
SQLFeatureNotSupportedException if the JDBC driver does not support this method or this method is not supported for the specified result set type and result set concurrency.

createStatement

Added in API level 1
Statement createStatement ()

创建一个用于将SQL语句发送到数据库的Statement对象。 不带参数的SQL语句通常使用Statement对象执行。 如果多次执行相同的SQL语句,则使用PreparedStatement对象可能更有效。

使用返回的Statement对象创建的结果集默认为TYPE_FORWARD_ONLY并且级别为CONCUR_READ_ONLY 创建的结果集的可保存性可以通过调用getHoldability()来确定。

Returns
Statement a new default Statement object
Throws
SQLException if a database access error occurs or this method is called on a closed connection

createStatement

Added in API level 1
Statement createStatement (int resultSetType, 
                int resultSetConcurrency, 
                int resultSetHoldability)

创建一个Statement对象,该对象将生成具有给定类型,并发性和可保存性的ResultSet对象。 此方法与createStatement方法createStatement相同,但它允许覆盖默认结果集类型,并发性和可保存性。

Parameters
resultSetType int: one of the following ResultSet constants: ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency int: one of the following ResultSet constants: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
resultSetHoldability int: one of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Returns
Statement a new Statement object that will generate ResultSet objects with the given type, concurrency, and holdability
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameters are not ResultSet constants indicating type, concurrency, and holdability
SQLFeatureNotSupportedException if the JDBC driver does not support this method or this method is not supported for the specified result set type, result set holdability and result set concurrency.

也可以看看:

createStruct

Added in API level 9
Struct createStruct (String typeName, 
                Object[] attributes)

用于创建Struct对象的工厂方法。

Parameters
typeName String: the SQL type name of the SQL structured type that this Struct object maps to. The typeName is the name of a user-defined type that has been defined for this database. It is the value returned by Struct.getSQLTypeName.
attributes Object: the attributes that populate the returned object
Returns
Struct a Struct object that maps to the given SQL type and is populated with the given attributes
Throws
SQLException if a database error occurs, the typeName is null or this method is called on a closed connection
SQLFeatureNotSupportedException if the JDBC driver does not support this data type

getAutoCommit

Added in API level 1
boolean getAutoCommit ()

检索此 Connection对象的当前自动提交模式。

Returns
boolean the current state of this Connection object's auto-commit mode
Throws
SQLException if a database access error occurs or this method is called on a closed connection

也可以看看:

getCatalog

Added in API level 1
String getCatalog ()

获取此 Connection对象的当前目录名称。

Returns
String the current catalog name or null if there is none
Throws
SQLException if a database access error occurs or this method is called on a closed connection

也可以看看:

getClientInfo

Added in API level 9
String getClientInfo (String name)

返回按名称指定的客户端信息属性的值。 如果指定的客户端信息属性尚未设置并且没有默认值,则此方法可能会返回null。 如果驱动程序不支持指定的客户端信息属性名称,则此方法也会返回null。

应用程序可以使用 DatabaseMetaData.getClientInfoProperties方法来确定驱动程序支持的客户端信息属性。

Parameters
name String: The name of the client info property to retrieve

Returns
String The value of the client info property specified

Throws
SQLException if the database server returns an error when fetching the client info value from the database or this method is called on a closed connection

也可以看看:

getClientInfo

Added in API level 9
Properties getClientInfo ()

返回包含驱动程序支持的每个客户端信息属性的名称和当前值的列表。 如果属性尚未设置并且没有默认值,则客户端信息属性的值可能为空。

Returns
Properties A Properties object that contains the name and current value of each of the client info properties supported by the driver.

Throws
SQLException if the database server returns an error when fetching the client info values from the database or this method is called on a closed connection

getHoldability

Added in API level 1
int getHoldability ()

检索使用此 Connection对象创建的 ResultSet对象的当前可保存性。

Returns
int the holdability, one of ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Throws
SQLException if a database access error occurs or this method is called on a closed connection

也可以看看:

getMetaData

Added in API level 1
DatabaseMetaData getMetaData ()

检索DatabaseMetaData对象包含有关哪个这个数据库的元数据Connection对象表示的连接。 元数据包括有关数据库表的信息,其支持的SQL语法,其存储过程,此连接的功能等。

Returns
DatabaseMetaData a DatabaseMetaData object for this Connection object
Throws
SQLException if a database access error occurs or this method is called on a closed connection

getTransactionIsolation

Added in API level 1
int getTransactionIsolation ()

检索此 Connection对象的当前事务隔离级别。

Returns
int the current transaction isolation level, which will be one of the following constants: Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, Connection.TRANSACTION_SERIALIZABLE, or Connection.TRANSACTION_NONE.
Throws
SQLException if a database access error occurs or this method is called on a closed connection

也可以看看:

getTypeMap

Added in API level 1
Map<StringClass<?>> getTypeMap ()

检索Map与此相关联的对象Connection对象。 除非应用程序添加了条目,否则返回的类型映射将为空。

你必须调用 setTypeMap在更改后 Map从返回的对象 getTypeMap为JDBC驱动程序可以创建的内部副本 Map传递给对象 setTypeMap

      Map<String,Class<?>> myMap = con.getTypeMap();
      myMap.put("mySchemaName.ATHLETES", Athletes.class);
      con.setTypeMap(myMap);
 

Returns
Map<StringClass<?>> the java.util.Map object associated with this Connection object
Throws
SQLException if a database access error occurs or this method is called on a closed connection
SQLFeatureNotSupportedException if the JDBC driver does not support this method

也可以看看:

getWarnings

Added in API level 1
SQLWarning getWarnings ()

检索此Connection对象通过调用报告的第一个警告。 如果有多个警告,后面的警告将被链接到第一个警告,并且可以通过调用先前检索到的警告的方法SQLWarning.getNextWarning来检索。

此方法不能在关闭的连接上调用; 这样做会导致SQLException被抛出。

注意:后续警告将被链接到此SQLWarning。

Returns
SQLWarning the first SQLWarning object or null if there are none
Throws
SQLException if a database access error occurs or this method is called on a closed connection

也可以看看:

isClosed

Added in API level 1
boolean isClosed ()

检索此Connection对象是否已关闭。 如果已调用方法close或者发生了某些致命错误,则连接将关闭。 只有在调用方法Connection.close调用此方法,才能保证返回true

通常不能调用此方法来确定与数据库的连接是有效还是无效。 典型的客户端可以通过捕获尝试操作时可能抛出的任何异常来确定连接是无效的。

Returns
boolean true if this Connection object is closed; false if it is still open
Throws
SQLException if a database access error occurs

isReadOnly

Added in API level 1
boolean isReadOnly ()

检索此 Connection对象是否处于只读模式。

Returns
boolean true if this Connection object is read-only; false otherwise
Throws
SQLException SQLException if a database access error occurs or this method is called on a closed connection

isValid

Added in API level 9
boolean isValid (int timeout)

如果连接尚未关闭并且仍然有效,则返回true。 驱动程序应该提交一个关于连接的查询或者使用一些其他的机制,当这个方法被调用时肯定地验证连接仍然有效。

驱动程序提交的用于验证连接的查询应当在当前事务的上下文中执行。

Parameters
timeout int: - The time in seconds to wait for the database operation used to validate the connection to complete. If the timeout period expires before the operation completes, this method returns false. A value of 0 indicates a timeout is not applied to the database operation.

Returns
boolean true if the connection is valid, false otherwise
Throws
SQLException if the value supplied for timeout is less then 0

也可以看看:

nativeSQL

Added in API level 1
String nativeSQL (String sql)

将给定的SQL语句转换为系统的原生SQL语法。 在发送之前,驱动程序可能会将JDBC SQL语法转换为其系统的原生SQL语法。 此方法返回驱动程序将发送的语句的本机形式。

Parameters
sql String: an SQL statement that may contain one or more '?' parameter placeholders
Returns
String the native form of this statement
Throws
SQLException if a database access error occurs or this method is called on a closed connection

prepareCall

Added in API level 1
CallableStatement prepareCall (String sql, 
                int resultSetType, 
                int resultSetConcurrency)

创建一个CallableStatement对象,该对象将生成具有给定类型和并发性的ResultSet对象。 此方法与上面的prepareCall方法相同,但它允许覆盖默认结果集类型和并发性。 创建的结果集的可保存性可以通过调用getHoldability()来确定。

Parameters
sql String: a String object that is the SQL statement to be sent to the database; may contain on or more '?' parameters
resultSetType int: a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency int: a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
Returns
CallableStatement a new CallableStatement object containing the pre-compiled SQL statement that will produce ResultSet objects with the given type and concurrency
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameters are not ResultSet constants indicating type and concurrency
SQLFeatureNotSupportedException if the JDBC driver does not support this method or this method is not supported for the specified result set type and result set concurrency.

prepareCall

Added in API level 1
CallableStatement prepareCall (String sql)

为调用数据库存储过程创建一个CallableStatement对象。 CallableStatement对象提供了设置其IN和OUT参数的方法,以及执行对存储过程调用的方法。

注意:此方法针对处理存储过程调用语句进行了优化。 当方法prepareCall完成时,某些驱动程序可能会将调用语句发送到数据库; 其他人可能会等到CallableStatement对象被执行。 这对用户没有直接影响; 但是,它会影响哪个方法抛出某些SQLExceptions。

使用返回的CallableStatement对象创建的结果集默认为TYPE_FORWARD_ONLY并且级别为CONCUR_READ_ONLY 创建的结果集的可保存性可以通过调用getHoldability()来确定。

Parameters
sql String: an SQL statement that may contain one or more '?' parameter placeholders. Typically this statement is specified using JDBC call escape syntax.
Returns
CallableStatement a new default CallableStatement object containing the pre-compiled SQL statement
Throws
SQLException if a database access error occurs or this method is called on a closed connection

prepareCall

Added in API level 1
CallableStatement prepareCall (String sql, 
                int resultSetType, 
                int resultSetConcurrency, 
                int resultSetHoldability)

创建一个CallableStatement对象,该对象将生成具有给定类型和并发性的ResultSet对象。 此方法与prepareCall方法prepareCall相同,但它允许重写默认结果集类型,结果集并发类型和可保留性。

Parameters
sql String: a String object that is the SQL statement to be sent to the database; may contain on or more '?' parameters
resultSetType int: one of the following ResultSet constants: ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency int: one of the following ResultSet constants: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
resultSetHoldability int: one of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Returns
CallableStatement a new CallableStatement object, containing the pre-compiled SQL statement, that will generate ResultSet objects with the given type, concurrency, and holdability
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameters are not ResultSet constants indicating type, concurrency, and holdability
SQLFeatureNotSupportedException if the JDBC driver does not support this method or this method is not supported for the specified result set type, result set holdability and result set concurrency.

也可以看看:

prepareStatement

Added in API level 1
PreparedStatement prepareStatement (String sql, 
                String[] columnNames)

创建一个默认的PreparedStatement对象,能够返回给定数组指定的自动生成的键。 该数组包含目标表中包含应该返回的自动生成的键的列的名称。 如果SQL语句不是INSERT语句,或者SQL语句能够返回自动生成的键(这些语句的列表是特定于供应商的),驱动程序将忽略数组。

有或没有IN参数的SQL语句可以预先编译并存储在一个PreparedStatement对象中。 然后可以使用此对象多次高效地执行此语句。

注意:此方法针对处理从预编译中受益的参数化SQL语句进行了优化。 如果驱动程序支持预编译,方法prepareStatement会将语句发送到数据库进行预编译。 某些驱动程序可能不支持预编译。 在这种情况下,执行PreparedStatement对象之前,可能不会将语句发送到数据库。 这对用户没有直接影响; 但是,它确实会影响哪些方法会抛出某些SQLException。

使用返回的PreparedStatement对象创建的结果集默认为TYPE_FORWARD_ONLY并且级别为CONCUR_READ_ONLY 创建的结果集的可保存性可以通过调用getHoldability()来确定。

Parameters
sql String: an SQL statement that may contain one or more '?' IN parameter placeholders
columnNames String: an array of column names indicating the columns that should be returned from the inserted row or rows
Returns
PreparedStatement a new PreparedStatement object, containing the pre-compiled statement, that is capable of returning the auto-generated keys designated by the given array of column names
Throws
SQLException if a database access error occurs or this method is called on a closed connection
SQLFeatureNotSupportedException if the JDBC driver does not support this method

prepareStatement

Added in API level 1
PreparedStatement prepareStatement (String sql, 
                int resultSetType, 
                int resultSetConcurrency, 
                int resultSetHoldability)

创建一个 PreparedStatement对象,该对象将生成具有给定类型,并发性和可保存性的 ResultSet对象。

此方法与 prepareStatement方法 prepareStatement相同,但它允许覆盖默认结果集类型,并发性和可保存性。

Parameters
sql String: a String object that is the SQL statement to be sent to the database; may contain one or more '?' IN parameters
resultSetType int: one of the following ResultSet constants: ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency int: one of the following ResultSet constants: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
resultSetHoldability int: one of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Returns
PreparedStatement a new PreparedStatement object, containing the pre-compiled SQL statement, that will generate ResultSet objects with the given type, concurrency, and holdability
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameters are not ResultSet constants indicating type, concurrency, and holdability
SQLFeatureNotSupportedException if the JDBC driver does not support this method or this method is not supported for the specified result set type, result set holdability and result set concurrency.

也可以看看:

prepareStatement

Added in API level 1
PreparedStatement prepareStatement (String sql, 
                int[] columnIndexes)

创建一个默认的PreparedStatement对象,能够返回给定数组指定的自动生成的键。 此数组包含目标表中包含应该使其可用的自动生成的键的列索引。 如果SQL语句不是INSERT语句,或者SQL语句能够返回自动生成的键(这些语句的列表是特定于供应商的),驱动程序将忽略该数组。

有或没有IN参数的SQL语句可以预先编译并存储在一个PreparedStatement对象中。 然后可以使用此对象多次高效地执行此语句。

注意:此方法针对处理从预编译中受益的参数化SQL语句进行了优化。 如果驱动程序支持预编译,则方法prepareStatement将将语句发送到数据库进行预编译。 某些驱动程序可能不支持预编译。 在这种情况下,执行PreparedStatement对象之前,该语句可能不会发送到数据库。 这对用户没有直接影响; 但是,它确实会影响哪些方法会抛出某些SQLException。

使用返回的PreparedStatement对象创建的结果集默认为TYPE_FORWARD_ONLY并且级别为CONCUR_READ_ONLY 创建的结果集的可保存性可以通过调用getHoldability()来确定。

Parameters
sql String: an SQL statement that may contain one or more '?' IN parameter placeholders
columnIndexes int: an array of column indexes indicating the columns that should be returned from the inserted row or rows
Returns
PreparedStatement a new PreparedStatement object, containing the pre-compiled statement, that is capable of returning the auto-generated keys designated by the given array of column indexes
Throws
SQLException if a database access error occurs or this method is called on a closed connection
SQLFeatureNotSupportedException if the JDBC driver does not support this method

prepareStatement

Added in API level 1
PreparedStatement prepareStatement (String sql)

创建一个用于将参数化SQL语句发送到数据库的 PreparedStatement对象。

有或没有IN参数的SQL语句可以预先编译并存储在一个PreparedStatement对象中。 然后可以使用此对象多次高效地执行此语句。

注意:此方法针对处理从预编译中受益的参数化SQL语句进行了优化。 如果驱动程序支持预编译,则方法prepareStatement会将语句发送到数据库进行预编译。 某些驱动程序可能不支持预编译。 在这种情况下,执行PreparedStatement对象之前,语句可能不会发送到数据库。 这对用户没有直接影响; 但是,它确实会影响哪些方法会抛出某些SQLException对象。

使用返回的创建结果集PreparedStatement对象将被默认类型TYPE_FORWARD_ONLY ,并有并发级别CONCUR_READ_ONLY 创建的结果集的可保存性可以通过调用getHoldability()来确定。

Parameters
sql String: an SQL statement that may contain one or more '?' IN parameter placeholders
Returns
PreparedStatement a new default PreparedStatement object containing the pre-compiled SQL statement
Throws
SQLException if a database access error occurs or this method is called on a closed connection

prepareStatement

Added in API level 1
PreparedStatement prepareStatement (String sql, 
                int autoGeneratedKeys)

创建一个能够检索自动生成的键的默认PreparedStatement对象。 给定的常量告诉驱动程序是否应该使自动生成的密钥可供检索。 如果SQL语句不是INSERT语句或能够返回自动生成键的SQL语句(这些语句的列表是特定于供应商的),则忽略此参数。

注意:此方法针对处理从预编译中受益的参数化SQL语句进行了优化。 如果驱动程序支持预编译,方法prepareStatement会将语句发送到数据库进行预编译。 某些驱动程序可能不支持预编译。 在这种情况下,执行PreparedStatement对象之前,可能不会将语句发送到数据库。 这对用户没有直接影响; 但是,它确实会影响哪些方法会抛出某些SQLException。

使用返回的PreparedStatement对象创建的结果集默认为TYPE_FORWARD_ONLY并且级别为CONCUR_READ_ONLY 创建的结果集的可保存性可以通过调用getHoldability()来确定。

Parameters
sql String: an SQL statement that may contain one or more '?' IN parameter placeholders
autoGeneratedKeys int: a flag indicating whether auto-generated keys should be returned; one of Statement.RETURN_GENERATED_KEYS or Statement.NO_GENERATED_KEYS
Returns
PreparedStatement a new PreparedStatement object, containing the pre-compiled SQL statement, that will have the capability of returning auto-generated keys
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameter is not a Statement constant indicating whether auto-generated keys should be returned
SQLFeatureNotSupportedException if the JDBC driver does not support this method with a constant of Statement.RETURN_GENERATED_KEYS

prepareStatement

Added in API level 1
PreparedStatement prepareStatement (String sql, 
                int resultSetType, 
                int resultSetConcurrency)

创建一个PreparedStatement对象,该对象将生成具有给定类型和并发性的ResultSet对象。 此方法与prepareStatement方法prepareStatement相同,但它允许覆盖默认结果集类型和并发性。 创建结果集的可保存性可通过调用getHoldability()来确定。

Parameters
sql String: a String object that is the SQL statement to be sent to the database; may contain one or more '?' IN parameters
resultSetType int: a result set type; one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
resultSetConcurrency int: a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
Returns
PreparedStatement a new PreparedStatement object containing the pre-compiled SQL statement that will produce ResultSet objects with the given type and concurrency
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameters are not ResultSet constants indicating type and concurrency
SQLFeatureNotSupportedException if the JDBC driver does not support this method or this method is not supported for the specified result set type and result set concurrency.

releaseSavepoint

Added in API level 1
void releaseSavepoint (Savepoint savepoint)

从当前事务中删除指定的Savepoint和随后的Savepoint对象。 在保存点被删除后对它的任何引用都会导致SQLException被引发。

Parameters
savepoint Savepoint: the Savepoint object to be removed
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given Savepoint object is not a valid savepoint in the current transaction
SQLFeatureNotSupportedException if the JDBC driver does not support this method

rollback

Added in API level 1
void rollback ()

取消当前事务中所做的所有更改并释放当前由此Connection对象持有的任何数据库锁定。 只有在禁用自动提交模式时才应使用此方法。

Throws
SQLException if a database access error occurs, this method is called while participating in a distributed transaction, this method is called on a closed connection or this Connection object is in auto-commit mode

也可以看看:

rollback

Added in API level 1
void rollback (Savepoint savepoint)

取消给定的 Savepoint对象设置后所做的所有更改。

只有在自动提交被禁用时才应使用此方法。

Parameters
savepoint Savepoint: the Savepoint object to roll back to
Throws
SQLException if a database access error occurs, this method is called while participating in a distributed transaction, this method is called on a closed connection, the Savepoint object is no longer valid, or this Connection object is currently in auto-commit mode
SQLFeatureNotSupportedException if the JDBC driver does not support this method

也可以看看:

setAutoCommit

Added in API level 1
void setAutoCommit (boolean autoCommit)

将此连接的自动提交模式设置为给定状态。 如果连接处于自动提交模式,则其所有SQL语句将作为单个事务执行并提交。 否则,它的SQL语句会被分组为通过调用方法commit或方法rollback终止的事务。 默认情况下,新连接处于自动提交模式。

语句完成时发生提交。 语句完成的时间取决于SQL语句的类型:

  • For DML statements, such as Insert, Update or Delete, and DDL statements, the statement is complete as soon as it has finished executing.
  • For Select statements, the statement is complete when the associated result set is closed.
  • For CallableStatement objects or for statements that return multiple results, the statement is complete when all of the associated result sets have been closed, and all update counts and output parameters have been retrieved.

注意:如果在事务期间调用此方法并且更改了自动提交模式,则事务将被提交。 如果setAutoCommit且自动提交模式未更改,则该调用为空操作。

Parameters
autoCommit boolean: true to enable auto-commit mode; false to disable it
Throws
SQLException if a database access error occurs, setAutoCommit(true) is called while participating in a distributed transaction, or this method is called on a closed connection

也可以看看:

setCatalog

Added in API level 1
void setCatalog (String catalog)

为了选择这个的子空间设置给定目录名称 Connection对象在其中工作的数据库。

如果驱动程序不支持目录,它将默默忽略此请求。

调用setCatalog对先前创建或准备的Statement对象没有影响。 实现定义了在调用Connection方法prepareStatementprepareCall时是否立即发生DBMS准备操作。 为了获得最大的可移植性, setCatalog应该在之前被称为Statement创建或准备。

Parameters
catalog String: the name of a catalog (subspace in this Connection object's database) in which to work
Throws
SQLException if a database access error occurs or this method is called on a closed connection

也可以看看:

setClientInfo

Added in API level 9
void setClientInfo (Properties properties)

设置连接的客户端信息属性的值。 Properties对象包含要设置的客户端信息属性的名称和值。 属性列表中包含的一组客户端信息属性将替换连接上的当前客户端信息属性集。 如果当前在连接上设置的属性不在属性列表中,则该属性将被清除。 指定一个空的属性列表将清除连接上的所有属性。 有关更多信息,请参阅setClientInfo (String, String)

如果在设置任何客户端信息属性时发生错误,则会引发SQLClientInfoException SQLClientInfoException包含指示哪些客户端信息属性未设置的信息。 客户端信息的状态是未知的,因为有些数据库不允许以原子方式设置多个客户端信息属性。 对于这些数据库,在发生错误之前可能已经设置了一个或多个属性。

Parameters
properties Properties: the list of client info properties to set

Throws
SQLClientInfoException if the database server returns an error while setting the clientInfo values on the database server or this method is called on a closed connection

也可以看看:

setClientInfo

Added in API level 9
void setClientInfo (String name, 
                String value)

将名称指定的客户端信息属性的值设置为值指定的值。

应用程序可以使用 DatabaseMetaData.getClientInfoProperties方法来确定驱动程序支持的客户端信息属性以及可以为每个属性指定的最大长度。

驱动程序将指定的值存储在数据库的合适位置。 例如在特殊的寄存器,会话参数或系统表列中。 为了提高效率,驱动程序可以推迟设置数据库中的值直到下一次执行或准备好语句。 除了将客户端信息存储在数据库中的适当位置之外,这些方法不应该改变连接的行为。 提供给这些方法的值仅用于记帐,诊断和调试目的。

如果驱动程序无法识别指定的客户端信息名称,驱动程序将生成警告。

如果为此方法指定的值大于属性的最大长度,则驱动程序可能会截断该值并生成警告或生成SQLClientInfoException 如果驱动程序生成SQLClientInfoException ,则指定的值未在连接上设置。

以下是标准客户端信息属性。 不需要驱动程序来支持这些属性,但是如果驱动程序支持可以由其中一个标准属性描述的客户端信息属性,则应该使用标准属性名称。

  • ApplicationName - The name of the application currently utilizing the connection
  • ClientUser - The name of the user that the application using the connection is performing work for. This may not be the same as the user name that was used in establishing the connection.
  • ClientHostname - The hostname of the computer the application using the connection is running on.

Parameters
name String: The name of the client info property to set
value String: The value to set the client info property to. If the value is null, the current value of the specified property is cleared.

Throws
SQLClientInfoException if the database server returns an error while setting the client info value on the database server or this method is called on a closed connection

setHoldability

Added in API level 1
void setHoldability (int holdability)

将使用此Connection对象创建的ResultSet对象的默认可保存性更改为给定的可保存性。 ResultSet对象的默认可保存性可通过调用getResultSetHoldability()来确定。

Parameters
holdability int: a ResultSet holdability constant; one of ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
Throws
SQLException if a database access occurs, this method is called on a closed connection, or the given parameter is not a ResultSet constant indicating holdability
SQLFeatureNotSupportedException if the given holdability is not supported

也可以看看:

setReadOnly

Added in API level 1
void setReadOnly (boolean readOnly)

将此连接置于只读模式,作为驱动程序启动数据库优化的提示。

注意:在交易过程中不能调用此方法。

Parameters
readOnly boolean: true enables read-only mode; false disables it
Throws
SQLException if a database access error occurs, this method is called on a closed connection or this method is called during a transaction

setSavepoint

Added in API level 1
Savepoint setSavepoint (String name)

使用当前事务中的给定名称创建一个保存点,并返回表示它的新对象 Savepoint

如果在活动事务之外调用setSavepoint,则将在此新创建的保存点上启动事务。

Parameters
name String: a String containing the name of the savepoint
Returns
Savepoint the new Savepoint object
Throws
SQLException if a database access error occurs, this method is called while participating in a distributed transaction, this method is called on a closed connection or this Connection object is currently in auto-commit mode
SQLFeatureNotSupportedException if the JDBC driver does not support this method

也可以看看:

setSavepoint

Added in API level 1
Savepoint setSavepoint ()

在当前事务中创建一个未命名的保存点,并返回表示它的新对象 Savepoint

如果在活动事务之外调用setSavepoint,则将在此新创建的保存点上启动事务。

Returns
Savepoint the new Savepoint object
Throws
SQLException if a database access error occurs, this method is called while participating in a distributed transaction, this method is called on a closed connection or this Connection object is currently in auto-commit mode
SQLFeatureNotSupportedException if the JDBC driver does not support this method

也可以看看:

setTransactionIsolation

Added in API level 1
void setTransactionIsolation (int level)

尝试将此Connection对象的事务隔离级别更改为给定的对象。 界面Connection中定义的常量是可能的事务隔离级别。

注意:如果在事务期间调用此方法,则结果是实现定义的。

Parameters
level int: one of the following Connection constants: Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_REPEATABLE_READ, or Connection.TRANSACTION_SERIALIZABLE. (Note that Connection.TRANSACTION_NONE cannot be used because it specifies that transactions are not supported.)
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameter is not one of the Connection constants

也可以看看:

setTypeMap

Added in API level 1
void setTypeMap (Map<StringClass<?>> map)

安装给定TypeMap对象的类型映射此Connection对象。 类型映射将用于SQL结构化类型和不同类型的自定义映射。

你必须设置的值 TypeMap到callng前 setMap为JDBC驱动程序可以创建的内部副本 TypeMap

      Map myMap<String,Class<?>> = new HashMap<String,Class<?>>();
      myMap.put("mySchemaName.ATHLETES", Athletes.class);
      con.setTypeMap(myMap);
 

Parameters
map Map: the java.util.Map object to install as the replacement for this Connection object's default type map
Throws
SQLException if a database access error occurs, this method is called on a closed connection or the given parameter is not a java.util.Map object
SQLFeatureNotSupportedException if the JDBC driver does not support this method

也可以看看:

Hooray!