JDBC接口参考
JDBC接口是一套提供给用户的API方法,本节将对部分常用接口做具体描述,若涉及其他接口可参考JDK1.6(软件包)/JDBC4.0中相关内容。
java.sql.Connection
java.sql.Connection是数据库连接接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| close() | void | Yes |
| commit() | void | Yes |
| createStatement() | Statement | Yes |
| getAutoCommit() | boolean | Yes |
| getClientInfo() | Properties | Yes |
| getClientInfo(String name) | String | Yes |
| getTransactionIsolation() | int | Yes |
| isClosed() | boolean | Yes |
| isReadOnly() | boolean | Yes |
| prepareStatement(String sql) | PreparedStatement | Yes |
| rollback() | void | Yes |
| setAutoCommit(boolean autoCommit) | void | Yes |
| setClientInfo(Properties properties) | void | Yes |
| setClientInfo(String name,String value) | void | Yes |
接口内部默认使用自动提交模式,若通过setAutoCommit(false)关闭自动提交,将会导致后面执行的语句都受到显式事务包裹,数据库中不支持事务中执行的语句不能在此模式下执行。
java.sql.CallableStatement
java.sql.CallableStatement是存储过程执行接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| registerOutParameter(int parameterIndex, int type) | void | Yes |
| wasNull() | boolean | Yes |
| getString(int parameterIndex) | String | Yes |
| getBoolean(int parameterIndex) | boolean | Yes |
| getByte(int parameterIndex) | byte | Yes |
| getShort(int parameterIndex) | short | Yes |
| getInt(int parameterIndex) | int | Yes |
| getLong(int parameterIndex) | long | Yes |
| getFloat(int parameterIndex) | float | Yes |
| getDouble(int parameterIndex) | double | Yes |
| getBigDecimal(int parameterIndex) | BigDecimal | Yes |
| getBytes(int parameterIndex) | byte[] | Yes |
| getDate(int parameterIndex) | Date | Yes |
| getTime(int parameterIndex) | Time | Yes |
| getTimestamp(int parameterIndex) | Timestamp | Yes |
| getObject(int parameterIndex) | Object | Yes |
- 不允许含有OUT参数的语句执行批量操作。
- 以下方法是从java.sql.Statement继承而来:close,execute,executeQuery,executeUpdate,getConnection,getResultSet,getUpdateCount,isClosed,setMaxRows , setFetchSize。
- 以下方法是从java.sql.PreparedStatement继承而来:addBatch,clearParameters,execute,executeQuery,executeUpdate,getMetaData,setBigDecimal,setBoolean,setByte,setBytes,setDate,setDouble,setFloat,setInt,setLong,setNull,setObject,setString,setTime,setTimestamp 。
java.sql.DatabaseMetaData
java.sql.DatabaseMetaData是数据库对象定义接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) | ResultSet | Yes |
| getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) | ResultSet | Yes |
| getTableTypes() | ResultSet | Yes |
| getUserName() | String | Yes |
| isReadOnly() | boolean | Yes |
| nullsAreSortedHigh() | boolean | Yes |
| nullsAreSortedLow() | boolean | Yes |
| nullsAreSortedAtStart() | boolean | Yes |
| nullsAreSortedAtEnd() | boolean | Yes |
| getDatabaseProductName() | String | Yes |
| getDatabaseProductVersion() | String | Yes |
| getDriverName() | String | Yes |
| getDriverVersion() | String | Yes |
| getDriverMajorVersion() | int | Yes |
| getDriverMinorVersion() | int | Yes |
| usesLocalFiles() | boolean | Yes |
| usesLocalFilePerTable() | boolean | Yes |
| supportsMixedCaseIdentifiers() | boolean | Yes |
| storesUpperCaseIdentifiers() | boolean | Yes |
| storesLowerCaseIdentifiers() | boolean | Yes |
| supportsMixedCaseQuotedIdentifiers() | boolean | Yes |
| storesUpperCaseQuotedIdentifiers() | boolean | Yes |
| storesLowerCaseQuotedIdentifiers() | boolean | Yes |
| storesMixedCaseQuotedIdentifiers() | boolean | Yes |
| supportsAlterTableWithAddColumn() | boolean | Yes |
| supportsAlterTableWithDropColumn() | boolean | Yes |
| supportsColumnAliasing() | boolean | Yes |
| nullPlusNonNullIsNull() | boolean | Yes |
| supportsConvert() | boolean | Yes |
| supportsConvert(int fromType, int toType) | boolean | Yes |
| supportsTableCorrelationNames() | boolean | Yes |
| supportsDifferentTableCorrelationNames() | boolean | Yes |
| supportsExpressionsInOrderBy() | boolean | Yes |
| supportsOrderByUnrelated() | boolean | Yes |
| supportsGroupBy() | boolean | Yes |
| supportsGroupByUnrelated() | boolean | Yes |
| supportsGroupByBeyondSelect() | boolean | Yes |
| supportsLikeEscapeClause() | boolean | Yes |
| supportsMultipleResultSets() | boolean | Yes |
| supportsMultipleTransactions() | boolean | Yes |
| supportsNonNullableColumns() | boolean | Yes |
| supportsMinimumSQLGrammar() | boolean | Yes |
| supportsCoreSQLGrammar() | boolean | Yes |
| supportsExtendedSQLGrammar() | boolean | Yes |
| supportsANSI92EntryLevelSQL() | boolean | Yes |
| supportsANSI92IntermediateSQL() | boolean | Yes |
| supportsANSI92FullSQL() | boolean | Yes |
| supportsIntegrityEnhancementFacility() | boolean | Yes |
| supportsOuterJoins() | boolean | Yes |
| supportsFullOuterJoins() | boolean | Yes |
| supportsLimitedOuterJoins() | boolean | Yes |
| isCatalogAtStart() | boolean | Yes |
| supportsSchemasInDataManipulation() | boolean | Yes |
| supportsSavepoints() | boolean | Yes |
| supportsResultSetHoldability(int holdability) | boolean | Yes |
| getResultSetHoldability() | int | Yes |
| getDatabaseMajorVersion() | int | Yes |
| getDatabaseMinorVersion() | int | Yes |
| getJDBCMajorVersion() | int | Yes |
| getJDBCMinorVersion() | int | Yes |
java.sql.Driver
java.sql.Driver是数据库驱动接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| acceptsURL(String url) | boolean | Yes |
| connect(String url, Properties info) | Connection | Yes |
| jdbcCompliant() | boolean | Yes |
| getMajorVersion() | int | Yes |
| getMinorVersion() | int | Yes |
java.sql.PreparedStatement
java.sql.PreparedStatement是预处理语句接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| clearParameters() | void | Yes |
| execute() | boolean | Yes |
| executeQuery() | ResultSet | Yes |
| executeUpdate() | int | Yes |
| getMetaData() | ResultSetMetaData | Yes |
| setBoolean(int parameterIndex, boolean x) | void | Yes |
| setBigDecimal(int parameterIndex, BigDecimal x) | void | Yes |
| setByte(int parameterIndex, byte x) | void | Yes |
| setBytes(int parameterIndex, byte[] x) | void | Yes |
| setDate(int parameterIndex, Date x) | void | Yes |
| setDouble(int parameterIndex, double x) | void | Yes |
| setFloat(int parameterIndex, float x) | void | Yes |
| setInt(int parameterIndex, int x) | void | Yes |
| setLong(int parameterIndex, long x) | void | Yes |
| setNString(int parameterIndex, String value) | void | Yes |
| setShort(int parameterIndex, short x) | void | Yes |
| setString(int parameterIndex, String x) | void | Yes |
| addBatch() | void | Yes |
| executeBatch() | int[] | Yes |
| clearBatch() | void | Yes |
- addBatch()、execute()必须在clearBatch()之后才能执行。
- 调用executeBatch()方法并不会清除batch。用户必须显式使用clearBatch()清除 。
- 在添加了一个batch的绑定变量后,用户若想重用这些值(再次添加一个batch),无需再次使用set*()方法 。
- 以下方法是从java.sql.Statement继承而来:close,execute,executeQuery,executeUpdate,getConnection,getResultSet,getUpdateCount,isClosed,setMaxRows, setFetchSize。
java.sql.ResultSet
java.sql.ResultSet是执行结果集接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| findColumn(String columnLabel) | int | Yes |
| getBigDecimal(int columnIndex) | BigDecimal | Yes |
| getBigDecimal(String columnLabel) | BigDecimal | Yes |
| getBoolean(int columnIndex) | boolean | Yes |
| getBoolean(String columnLabel) | boolean | Yes |
| getByte(int columnIndex) | byte | Yes |
| getBytes(int columnIndex) | byte[] | Yes |
| getByte(String columnLabel) | byte | Yes |
| getBytes(String columnLabel) | byte[] | Yes |
| getDate(int columnIndex) | Date | Yes |
| getDate(String columnLabel) | Date | Yes |
| getDouble(int columnIndex) | double | Yes |
| getDouble(String columnLabel) | double | Yes |
| getFloat(int columnIndex) | float | Yes |
| getFloat(String columnLabel) | float | Yes |
| getInt(int columnIndex) | int | Yes |
| getInt(String columnLabel) | int | Yes |
| getLong(int columnIndex) | long | Yes |
| getLong(String columnLabel) | long | Yes |
| getShort(int columnIndex) | short | Yes |
| getShort(String columnLabel) | short | Yes |
| getString(int columnIndex) | String | Yes |
| getString(String columnLabel) | String | Yes |
| getTime(int columnIndex) | Time | Yes |
| getTime(String columnLabel) | Time | Yes |
| getTimestamp(int columnIndex) | Timestamp | Yes |
| getTimestamp(String columnLabel) | Timestamp | Yes |
| isAfterLast() | boolean | Yes |
| isBeforeFirst() | boolean | Yes |
| isFirst() | boolean | Yes |
| next() | boolean | Yes |
- 一个Statement不能有多个处于“open”状态的ResultSet。
- 用于遍历结果集(ResultSet)的游标(Cursor)在被提交后不能保持“open”的状态。
java.sql.ResultSetMetaData
java.sql.ResultSetMetaData是对ResultSet对象相关信息的具体描述。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| getColumnCount() | int | Yes |
| getColumnName(int column) | String | Yes |
| getColumnType(int column) | int | Yes |
| getColumnTypeName(int column) | String | Yes |
java.sql.Statement
java.sql.Statement是SQL语句接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| close() | void | Yes |
| execute(String sql) | boolean | Yes |
| executeQuery(String sql) | ResultSet | Yes |
| executeUpdate(String sql) | int | Yes |
| getConnection() | Connection | Yes |
| getResultSet() | ResultSet | Yes |
| getQueryTimeout() | int | Yes |
| getUpdateCount() | int | Yes |
| isClosed() | boolean | Yes |
| setQueryTimeout(int seconds) | void | Yes |
| setFetchSize(int rows) | void | Yes |
| cancel() | void | Yes |
通过setFetchSize可以减少结果集在客户端的内存占用情况。它的原理是通过将结果集打包成游标,然后分段处理,所以会加大数据库与客户端的通信量,会有性能损耗。
由于数据库游标是事务内有效,所以,在设置setFetchSize的同时,需要将连接设置为非自动提交模式,setAutoCommit(false)。同时在业务数据需要持久化到数据库中时,在连接上执行提交操作。
javax.sql.ConnectionPoolDataSource
javax.sql.ConnectionPoolDataSource是数据源连接池接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| getLoginTimeout() | int | Yes |
| getLogWriter() | PrintWriter | Yes |
| getPooledConnection() | PooledConnection | Yes |
| getPooledConnection(String user,String password) | PooledConnection | Yes |
| setLoginTimeout(int seconds) | void | Yes |
| setLogWriter(PrintWriter out) | void | Yes |
javax.sql.DataSource
javax.sql.DataSource是数据源接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| getConnection() | Connection | Yes |
| getConnection(String username,String password) | Connection | Yes |
| getLoginTimeout() | int | Yes |
| getLogWriter() | PrintWriter | Yes |
| setLoginTimeout(int seconds) | void | Yes |
| setLogWriter(PrintWriter out) | void | Yes |
javax.sql.PooledConnection
javax.sql.PooledConnection是由连接池创建的连接接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| addConnectionEventListener (ConnectionEventListener listener) | void | Yes |
| close() | void | Yes |
| getConnection() | Connection | Yes |
| removeConnectionEventListener (ConnectionEventListener listener) | void | Yes |
| addStatementEventListener (StatementEventListener listener) | void | Yes |
| removeStatementEventListener (StatementEventListener listener) | void | Yes |
javax.naming.Context
javax.naming.Context是连接配置的上下文接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| bind(Name name, Object obj) | void | Yes |
| bind(String name, Object obj) | void | Yes |
| lookup(Name name) | Object | Yes |
| lookup(String name) | Object | Yes |
| rebind(Name name, Object obj) | void | Yes |
| rebind(String name, Object obj) | void | Yes |
| rename(Name oldName, Name newName) | void | Yes |
| rename(String oldName, String newName) | void | Yes |
| unbind(Name name) | void | Yes |
| unbind(String name) | void | Yes |
javax.naming.spi.InitialContextFactory
javax.naming.spi.InitialContextFactory是初始连接上下文工厂接口。
| 方法名 | 返回值类型 | 支持JDBC 4 |
|---|---|---|
| getInitialContext(Hashtable<?,?> environment) | Context | Yes |
CopyManager
CopyManager是DWS JDBC驱动中提供的一个API接口类,用于批量向DWS集群中导入数据。
CopyManager的继承关系
CopyManager类位于org.postgresql.copy Package中,继承自java.lang.Object类,该类的声明如下:
public class CopyManager extends Object
构造方法
public CopyManager(BaseConnection connection) throws SQLException
常用方法
| 返回值 | 方法 | 描述 | throws |
|---|---|---|---|
| CopyIn | copyIn(String sql) | - | SQLException |
| long | copyIn(String sql, InputStream from) | 使用COPY FROM STDIN从InputStream中快速向数据库中的表加载数据。 | SQLException,IOException |
| long | copyIn(String sql, InputStream from, int bufferSize) | 使用COPY FROM STDIN从InputStream中快速向数据库中的表加载数据。 | SQLException,IOException |
| long | copyIn(String sql, Reader from) | 使用COPY FROM STDIN从Reader中快速向数据库中的表加载数据。 | SQLException,IOException |
| long | copyIn(String sql, Reader from, int bufferSize) | 使用COPY FROM STDIN从Reader中快速向数据库中的表加载数据。 | SQLException,IOException |
| CopyOut | copyOut(String sql) | - | SQLException |
| long | copyOut(String sql, OutputStream to) | 将一个COPY TO STDOUT的结果集从数据库发送到OutputStream类中。 | SQLException,IOException |
| long | copyOut(String sql, Writer to) | 将一个COPY TO STDOUT的结果集从数据库发送到Writer类中。 | SQLException,IOException |