• 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.
     * 

    * 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 unwrap 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 SQLException is thrown. * * @param 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 isWrapperFor 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 unwrap so that * callers can use this method to avoid expensive unwrap calls that may fail. If this method * returns true then calling unwrap 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; }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    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;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    结语

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

  • 相关阅读:
    关于stm32的复用和重映射问题
    【苹果位置推相册推送】包括撤消受权或更改Apple软件中可访问服务的任何API)
    FFmpeg截图命令优化
    VueComponent的原型对象
    湖仓一体电商项目(十):业务实现之编写写入DWD层业务代码
    LeetCode刷题复盘笔记——491. 递增子序列(一文搞懂回溯解决递增子序列问题)
    C++官网 Tutorials C++ Language Basics of C++:Variables and types
    NDepend v2022.2.1.9665 专业版
    php单独使用think-rom数据库 | thinkphp手动关闭数据库连接
    带联网功能的RFID宿舍门禁(六)-两年后的再次总结
  • 原文地址:https://blog.csdn.net/weixin_45487988/article/details/126574822