JDBC 中的类与接口

序言

之前我们玩过了JDBC,我们会了?你想多了,这只是JDBC的冰山一角而已,接下来我们要全面回顾一下重要的类与接口。JDBC API由java.sql和javax.sql两个包构成,我们来学习一下这两兄弟海尔兄弟舒克与贝塔。

java.sql包

java.sql包中涵盖了JDBC最核心的API,下面是java.sql包中的所有接口,枚举和类:

数据类型:
java.sql.Array
java.sql.Blob
java.sql.Clob
java.sql.Date
java.sql.NClob
java.sql.Time
java.sql.TimeStamp
java.sql.SqlXml
java.sql.Ref
java.sql.RowId
java.sql.SQLOutput
java.sql.SQLData
java.sql.SQLInput

枚举
java.sql.SQLType
java.sql.JDBCType
java.sql.Types
java.sql.RowIdLifeTime
java.sql.PseudoColumnUsage
java.sql.ClientinfoStatus

Api相关
java.sql.Wrapper
java.sql.Connection
java.sql.Statement
java.sql.CallableStatement
java.sql.PreparedStatement
java.sql.DatabaseMetaData
java.sql.ParameterMetaData
java.sql.ResultSet
java.sql.ResultSetMetaData

驱动相关
java.sql.Driver
java.sql.DriverAction
java.sql.DriverManager
java.sql.DriverPropertyInfo
java.sql.SQLPermission
java.sql.Savepoint

异常
java.sql.BatchUpdateException
java.sql.DataTruncation
java.sql.SQLClientInfoException
java.sql.SQLDataException
java.sql.SQLException
java.sql.SQLFeatureNotSupportedException

其实java.sql中的类不算多,大致分为接口,异常,API,驱动,枚举类
除这几个部分,剩下的就是作为java开发人员需要掌握的API,主要包括下面几个:

java.sql.Wrapper
java.sql.Connection
java.sql.Statement
java.sql.CallableStatement
java.sql.PreparedStatement
java.sql.DatabaseMetaData
java.sql.ParameterMetaData
java.sql.ResultSet
java.sql.ResultSetMetaData

这些接口都继承了 java.sql.Wrapper接口

/*
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package java.sql;

/**
 * Interface for JDBC classes which provide the ability to retrieve the delegate instance when the instance
 * in question is in fact a proxy class.
 * <p>
 * The wrapper pattern is employed by many JDBC driver implementations to provide extensions beyond
 * the traditional JDBC API that are specific to a data source. Developers may wish to gain access to
 * these resources that are wrapped (the delegates) as  proxy class instances representing the
 * the actual resources. This interface describes a standard mechanism to access
 * these wrapped resources
 * represented by their proxy, to permit direct access to the resource delegates.
 *
 * @since 1.6
 */

public interface Wrapper {

    /**
     * Returns an object that implements the given interface to allow access to
     * non-standard methods, or standard methods not exposed by the proxy.
     *
     * If the receiver implements the interface then the result is the receiver
     * or a proxy for the receiver. If the receiver is a wrapper
     * and the wrapped object implements the interface then the result is the
     * wrapped object or a proxy for the wrapped object. Otherwise return the
     * the result of calling <code>unwrap</code> recursively on the wrapped object
     * or a proxy for that result. If the receiver is not a
     * wrapper and does not implement the interface, then an <code>SQLException</code> is thrown.
     *
     * @param <T> the type of the class modeled by this Class object
     * @param iface A Class defining an interface that the result must implement.
     * @return an object that implements the interface. May be a proxy for the actual implementing object.
     * @throws java.sql.SQLException If no object found that implements the interface
     * @since 1.6
     */
        <T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException;

    /**
     * Returns true if this either implements the interface argument or is directly or indirectly a wrapper
     * for an object that does. Returns false otherwise. If this implements the interface then return true,
     * else if this is a wrapper then return the result of recursively calling <code>isWrapperFor</code> on the wrapped
     * object. If this does not implement the interface and is not a wrapper, return false.
     * This method should be implemented as a low-cost operation compared to <code>unwrap</code> so that
     * callers can use this method to avoid expensive <code>unwrap</code> calls that may fail. If this method
     * returns true then calling <code>unwrap</code> with the same argument should succeed.
     *
     * @param iface a Class defining an interface.
     * @return true if this implements the interface or directly or indirectly wraps an object that does.
     * @throws java.sql.SQLException  if an error occurs while determining whether this is a wrapper
     * for an object with the given interface.
     * @since 1.6
     */
    boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException;

}

unwrap()方法主要用于返回未经过包装的JDBC驱动原始类型实例,我们通过该实例调用JDBC驱动中提供的非标准的方法。
isWrapperFor()方法主要用于判断当前实例是否是JDBC驱动中某一类型的包装类型。

JAVAX.SQL包详解

javax主要包括下面几个类和接口

数据源
javax.sql.DataSource
javax.sql.CommonDataSource

连接池相关
javax.sql.ConnectionPoolDataSource
javax.sql.PooledConnection
javax.sql.ConnectionEvent
javax.sql.ConnectionEventListener
javax.sql.StatementEvent
javax.sql.StatementEventListener

等等等等,不一一列举了
相较于java.sql中的Connection的产生,DataSource提供了一个更好的数据源链接方式。
最大的好处不需要再硬编码了。
再来 就是体现在连接池和分布式事务上。连接池的复用并不是每次都新建一个新的链接来玩,可以提高效率和内存的使用。
数据池实现模块可以调用PooledConnection对象的addConnectionEventListenner()将自己注册为一个PooledConnection对象的监听者。当数据库链接需要重用或者关闭的时候会产生一个ConnectionEvent对象,表示一个链接事件。

另外Javax.sql包中还包含XADataSource、XAResource和XAConnection接口,这些接口都提供了分布式事务的支持,具体由JDBCQ驱动来实现。

javax.sql包中还提供了一个Rowset接口,该接口继承自java.sql包中的ResultSet接口。RowSet用于为数据源和应用程序在内容中建立一个映射。RowSet是一个可滚动、可更新、可序列化的结果集,而且作为一个JavaBean的组件,可以直接同步数据源的数据。

Connection简介

一个Connection表示通过jdbc驱动和数据源建立的链接,这里的数据源可以是关系型数据库系统、文件系统或者其他JDBC驱动访问的数据。一个Connection对象可能访问多个数据源。
举个例子(伪代码)

@Resource(name = "jdbc/source1")
DataSource source1;

@Resource(name = "jdbc/source2")
DataSource source2;

final List<String> list = Lists.newArrayList("Source1", "Source2");

public void getresponse() throws Exception {

    for (String source : list) {
        Connection connection = null;
        Statement statement = null;
        try {
            connection = getConnection(source);
            statement = connection.createStatement();
        } catch (SQLException ex) {
        } finally {
            statement.close();
            connection.close();
        }
    }
}

public Connection getConnection(String source) {
    Connection conn = null;
    try {
    if(source.equalsIgnoreCase("Source1")){
        conn = source1.getConnection();
    } else if(source.equalsIgnoreCase("Source2")){
        conn = source2.getConnection();
    }
    } catch(SQLException se) {

    }

    return conn;
 }

结语

我们对jdbc中的类和接口做了介绍,并且对Connection做了一个简单的介绍,下一节对Connection展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值