• package java.sql description

    • What the JDBCTM 4.2 API Includes

    • Versions

    • What the java.sql Package Contains

      • Making a connection with a database via the DriverManager facility
      • Sending SQL statements to a database
      • Retrieving and updating the results of a query
      • Standard mappings for SQL types to classes and interfaces in the Java programming language
      • Custom mapping an SQL user-defined type (UDT) to a class in the Java programming language
      • Metadata
      • Exceptions
    • java.sql and javax.sql Features Introduced in the JDBC 4.2 API

    • java.sql and javax.sql Features Introduced in the JDBC 4.1 API

    • java.sql and javax.sql Features Introduced in the JDBC 4.0 API

    • java.sql and javax.sql Features Introduced in the JDBC 3.0 API

    • java.sql Features Introduced in the JDBC 2.1 Core API

    • javax.sql Features Introduced in the JDBC 2.0 Optional Package API

    • Custom Mapping of UDTs

  • Package javax.sql Description

    • Using a DataSource Object to Make a Connection

    • Connection Pooling and Statement Pooling

    • Distributed Transactions

    • Rowsets

      • Event Notification
      • Metadata
      • The Reader/Writer Facility

Package java.sql Description

Provides the API for accessing and processing data stored in a data source (usually a relational database) using the JavaTM programming language. This API includes a framework whereby different drivers can be installed dynamically to access different data sources. Although the JDBCTM API is mainly geared to passing SQL statements to a database, it provides for reading and writing data from any data source with a tabular format. The reader/writer facility, available through the javax.sql.RowSet group of interfaces, can be customized to use and update data from a spread sheet, flat file, or any other tabular data source.

What the JDBCTM 4.2 API Includes

The JDBCTM 4.2 API includes both the java.sql package, referred to as the JDBC core API, and the javax.sql package, referred to as the JDBC Optional Package API. This complete JDBC API is included in the JavaTM Standard Edition (Java SETM), version 7. The javax.sql package extends the functionality of the JDBC API from a client-side API to a server-side API, and it is an essential part of the JavaTM Enterprise Edition (Java EETM) technology.

Versions

The JDBC 4.2 API incorporates all of the previous JDBC API versions:

  • The JDBC 4.1 API
  • The JDBC 4.0 API
  • The JDBC 3.0 API
  • The JDBC 2.1 core API
  • The JDBC 2.0 Optional Package API
    (Note that the JDBC 2.1 core API and the JDBC 2.0 Optional Package API together are referred to as the JDBC 2.0 API.)
  • The JDBC 1.2 API
  • The JDBC 1.0 API

Classes, interfaces, methods, fields, constructors, and exceptions have the following "since" tags that indicate when they were introduced into the Java platform. When these "since" tags are used in JavadocTM comments for the JDBC API, they indicate the following:

  • Since 1.8 -- new in the JDBC 4.2 API and part of the Java SE platform, version 8
  • Since 1.7 -- new in the JDBC 4.1 API and part of the Java SE platform, version 7
  • Since 1.6 -- new in the JDBC 4.0 API and part of the Java SE platform, version 6
  • Since 1.4 -- new in the JDBC 3.0 API and part of the J2SE platform, version 1.4
  • Since 1.2 -- new in the JDBC 2.0 API and part of the J2SE platform, version 1.2
  • Since 1.1 or no "since" tag -- in the original JDBC 1.0 API and part of the JDKTM, version 1.1

NOTE: Many of the new features are optional; consequently, there is some variation in drivers and the features they support. Always check your driver's documentation to see whether it supports a feature before you try to use it.

NOTE: The class SQLPermission was added in the JavaTM 2 SDK, Standard Edition, version 1.3 release. This class is used to prevent unauthorized access to the logging stream associated with the DriverManager, which may contain information such as table names, column data, and so on.

What the java.sql Package Contains

The java.sql package contains API for the following:

  • Making a connection with a database via the DriverManager facility

    • DriverManager class -- makes a connection with a driver
    • SQLPermission class -- provides permission when code running within a Security Manager, such as an applet, attempts to set up a logging stream through the DriverManager
    • Driver interface -- provides the API for registering and connecting drivers based on JDBC technology ("JDBC drivers"); generally used only by the DriverManager class
    • DriverPropertyInfo class -- provides properties for a JDBC driver; not used by the general user
  • Sending SQL statements to a database
    • Statement -- used to send basic SQL statements
    • PreparedStatement -- used to send prepared statements or basic SQL statements (derived from Statement)
    • CallableStatement -- used to call database stored procedures (derived from PreparedStatement)
    • Connection interface -- provides methods for creating statements and managing connections and their properties
    • Savepoint -- provides savepoints in a transaction
  • Retrieving and updating the results of a query
    • ResultSet interface
  • Standard mappings for SQL types to classes and interfaces in the Java programming language
    • Array interface -- mapping for SQL ARRAY
    • Blob interface -- mapping for SQL BLOB
    • Clob interface -- mapping for SQL CLOB
    • Date class -- mapping for SQL DATE
    • NClob interface -- mapping for SQL NCLOB
    • Ref interface -- mapping for SQL REF
    • RowId interface -- mapping for SQL ROWID
    • Struct interface -- mapping for SQL STRUCT
    • SQLXML interface -- mapping for SQL XML
    • Time class -- mapping for SQL TIME
    • Timestamp class -- mapping for SQL TIMESTAMP
    • Types class -- provides constants for SQL types
  • Custom mapping an SQL user-defined type (UDT) to a class in the Java programming language
    • SQLData interface -- specifies the mapping of a UDT to an instance of this class
    • SQLInput interface -- provides methods for reading UDT attributes from a stream
    • SQLOutput interface -- provides methods for writing UDT attributes back to a stream
  • Metadata
    • DatabaseMetaData interface -- provides information about the database
    • ResultSetMetaData interface -- provides information about the columns of a ResultSet object
    • ParameterMetaData interface -- provides information about the parameters to PreparedStatement commands
  • Exceptions
    • SQLException -- thrown by most methods when there is a problem accessing data and by some methods for other reasons
    • SQLWarning -- thrown to indicate a warning
    • DataTruncation -- thrown to indicate that data may have been truncated
    • BatchUpdateException -- thrown to indicate that not all commands in a batch update executed successfully

java.sql and javax.sql Features Introduced in the JDBC 4.2 API

  • Added JDBCType enum and SQLType interface
  • Support for REF CURSORS in CallableStatement
  • DatabaseMetaData methods to return maximum Logical LOB size and if Ref Cursors are supported
  • Added support for large update counts

java.sql and javax.sql Features Introduced in the JDBC 4.1 API

  • Allow ConnectionResultSet and Statement objects to be used with the try-with-resources statement
  • Supported added to CallableStatement and ResultSet to specify the Java type to convert to via the getObject method
  • DatabaseMetaData methods to return PseudoColumns and if a generated key is always returned
  • Added support to Connection to specify a database schema, abort and timeout a physical connection.
  • Added support to close a Statement object when its dependent objects have been closed
  • Support for obtaining the parent logger for a DriverDataSourceConnectionPoolDataSource and XADataSource

java.sql and javax.sql Features Introduced in the JDBC 4.0 API

  • auto java.sql.Driver discovery -- no longer need to load a java.sql.Driver class via Class.forName
  • National Character Set support added
  • Support added for the SQL:2003 XML data type
  • SQLException enhancements -- Added support for cause chaining; New SQLExceptions added for common SQLState class value codes
  • Enhanced Blob/Clob functionality -- Support provided to create and free a Blob/Clob instance as well as additional methods added to improve accessibility
  • Support added for accessing a SQL ROWID
  • Support added to allow a JDBC application to access an instance of a JDBC resource that has been wrapped by a vendor, usually in an application server or connection pooling environment.
  • Availability to be notified when a PreparedStatement that is associated with a PooledConnection has been closed or the driver determines is invalid

java.sql and javax.sql Features Introduced in the JDBC 3.0 API

  • Pooled statements -- reuse of statements associated with a pooled connection
  • Savepoints -- allow a transaction to be rolled back to a designated savepoint
  • Properties defined for ConnectionPoolDataSource -- specify how connections are to be pooled
  • Metadata for parameters of a PreparedStatement object
  • Ability to retrieve values from automatically generated columns
  • Ability to have multiple ResultSet objects returned from CallableStatement objects open at the same time
  • Ability to identify parameters to CallableStatement objects by name as well as by index
  • ResultSet holdability -- ability to specify whether cursors should be held open or closed at the end of a transaction
  • Ability to retrieve and update the SQL structured type instance that a Ref object references
  • Ability to programmatically update BLOBCLOBARRAY, and REF values.
  • Addition of the java.sql.Types.DATALINK data type -- allows JDBC drivers access to objects stored outside a data source
  • Addition of metadata for retrieving SQL type hierarchies

java.sql Features Introduced in the JDBC 2.1 Core API

  • Scrollable result sets--using new methods in the ResultSet interface that allow the cursor to be moved to a particular row or to a position relative to its current position
  • Batch updates
  • Programmatic updates--using ResultSet updater methods
  • New data types--interfaces mapping the SQL3 data types
  • Custom mapping of user-defined types (UDTs)
  • Miscellaneous features, including performance hints, the use of character streams, full precision for java.math.BigDecimal values, additional security, and support for time zones in date, time, and timestamp values.

javax.sql Features Introduced in the JDBC 2.0 Optional Package API

  • The DataSource interface as a means of making a connection. The Java Naming and Directory InterfaceTM (JNDI) is used for registering a DataSource object with a naming service and also for retrieving it.
  • Pooled connections -- allowing connections to be used and reused
  • Distributed transactions -- allowing a transaction to span diverse DBMS servers
  • RowSet technology -- providing a convenient means of handling and passing data

Custom Mapping of UDTs

A user-defined type (UDT) defined in SQL can be mapped to a class in the Java programming language. An SQL structured type or an SQL DISTINCT type are the UDTs that may be custom mapped. The following three steps set up a custom mapping:

  1. Defining the SQL structured type or DISTINCT type in SQL
  2. Defining the class in the Java programming language to which the SQL UDT will be mapped. This class must implement the SQLData interface.
  3. Making an entry in a Connection object's type map that contains two things:
    • the fully-qualified SQL name of the UDT
    • the Class object for the class that implements the SQLData interface

When these are in place for a UDT, calling the methods ResultSet.getObject or CallableStatement.getObject on that UDT will automatically retrieve the custom mapping for it. Also, thePreparedStatement.setObject method will automatically map the object back to its SQL type to store it in the data source.

Package javax.sql Description

Provides the API for server side data source access and processing from the JavaTM programming language. This package supplements the java.sql package and, as of the version 1.4 release, is included in the Java Platform, Standard Edition (Java SETM). It remains an essential part of the Java Platform, Enterprise Edition (Java EETM).

The javax.sql package provides for the following:

  1. The DataSource interface as an alternative to the DriverManager for establishing a connection with a data source
  2. Connection pooling and Statement pooling
  3. Distributed transactions
  4. Rowsets

Applications use the DataSource and RowSet APIs directly, but the connection pooling and distributed transaction APIs are used internally by the middle-tier infrastructure.

Using a DataSource Object to Make a Connection

The javax.sql package provides the preferred way to make a connection with a data source. The DriverManager class, the original mechanism, is still valid, and code using it will continue to run. However, the newer DataSource mechanism is preferred because it offers many advantages over the DriverManager mechanism.

These are the main advantages of using a DataSource object to make a connection:

  • Changes can be made to a data source's properties, which means that it is not necessary to make changes in application code when something about the data source or driver changes.
  • Connection and Statement pooling and distributed transactions are available through a DataSource object that is implemented to work with the middle-tier infrastructure. Connections made through the DriverManager do not have connection and statement pooling or distributed transaction capabilities.

Driver vendors provide DataSource implementations. A particular DataSource object represents a particular physical data source, and each connection the DataSource object creates is a connection to that physical data source.

A logical name for the data source is registered with a naming service that uses the Java Naming and Directory InterfaceTM (JNDI) API, usually by a system administrator or someone performing the duties of a system administrator. An application can retrieve the DataSource object it wants by doing a lookup on the logical name that has been registered for it. The application can then use the DataSource object to create a connection to the physical data source it represents.

DataSource object can be implemented to work with the middle tier infrastructure so that the connections it produces will be pooled for reuse. An application that uses such a DataSourceimplementation will automatically get a connection that participates in connection pooling. A DataSource object can also be implemented to work with the middle tier infrastructure so that the connections it produces can be used for distributed transactions without any special coding.

Connection Pooling and Statement Pooling

Connections made via a DataSource object that is implemented to work with a middle tier connection pool manager will participate in connection pooling. This can improve performance dramatically because creating new connections is very expensive. Connection pooling allows a connection to be used and reused, thus cutting down substantially on the number of new connections that need to be created.

Connection pooling is totally transparent. It is done automatically in the middle tier of a Java EE configuration, so from an application's viewpoint, no change in code is required. An application simply uses the DataSource.getConnection method to get the pooled connection and uses it the same way it uses any Connection object.

The classes and interfaces used for connection pooling are:

  • ConnectionPoolDataSource
  • PooledConnection
  • ConnectionEvent
  • ConnectionEventListener
  • StatementEvent
  • StatementEventListener

The connection pool manager, a facility in the middle tier of a three-tier architecture, uses these classes and interfaces behind the scenes. When a ConnectionPoolDataSource object is called on to create a PooledConnection object, the connection pool manager will register as a ConnectionEventListener object with the new PooledConnection object. When the connection is closed or there is an error, the connection pool manager (being a listener) gets a notification that includes a ConnectionEvent object.

If the connection pool manager supports Statement pooling, for PreparedStatements, which can be determined by invoking the method DatabaseMetaData.supportsStatementPooling, the connection pool manager will register as a StatementEventListener object with the new PooledConnection object. When the PreparedStatement is closed or there is an error, the connection pool manager (being a listener) gets a notification that includes a StatementEvent object.

Distributed Transactions

As with pooled connections, connections made via a DataSource object that is implemented to work with the middle tier infrastructure may participate in distributed transactions. This gives an application the ability to involve data sources on multiple servers in a single transaction.

The classes and interfaces used for distributed transactions are:

  • XADataSource
  • XAConnection

These interfaces are used by the transaction manager; an application does not use them directly.

The XAConnection interface is derived from the PooledConnection interface, so what applies to a pooled connection also applies to a connection that is part of a distributed transaction. A transaction manager in the middle tier handles everything transparently. The only change in application code is that an application cannot do anything that would interfere with the transaction manager's handling of the transaction. Specifically, an application cannot call the methods Connection.commit or Connection.rollback, and it cannot set the connection to be in auto-commit mode (that is, it cannot call Connection.setAutoCommit(true)).

An application does not need to do anything special to participate in a distributed transaction. It simply creates connections to the data sources it wants to use via theDataSource.getConnection method, just as it normally does. The transaction manager manages the transaction behind the scenes. The XADataSource interface creates XAConnectionobjects, and each XAConnection object creates an XAResource object that the transaction manager uses to manage the connection.

Rowsets

The RowSet interface works with various other classes and interfaces behind the scenes. These can be grouped into three categories.

  1. Event Notification

    • RowSetListener
      RowSet object is a JavaBeansTM component because it has properties and participates in the JavaBeans event notification mechanism. The RowSetListener interface is implemented by a component that wants to be notified about events that occur to a particular RowSet object. Such a component registers itself as a listener with a rowset via theRowSet.addRowSetListener method.

      When the RowSet object changes one of its rows, changes all of it rows, or moves its cursor, it also notifies each listener that is registered with it. The listener reacts by carrying out its implementation of the notification method called on it.

    • RowSetEvent
      As part of its internal notification process, a RowSet object creates an instance of RowSetEvent and passes it to the listener. The listener can use this RowSetEvent object to find out which rowset had the event.
  2. Metadata
    • RowSetMetaData
      This interface, derived from the ResultSetMetaData interface, provides information about the columns in a RowSet object. An application can use RowSetMetaData methods to find out how many columns the rowset contains and what kind of data each column can contain.

      The RowSetMetaData interface provides methods for setting the information about columns, but an application would not normally use these methods. When an application calls the RowSet method execute, the RowSet object will contain a new set of rows, and its RowSetMetaData object will have been internally updated to contain information about the new columns.

  3. The Reader/Writer Facility
    RowSet object that implements the RowSetInternal interface can call on the RowSetReader object associated with it to populate itself with data. It can also call on the RowSetWriterobject associated with it to write any changes to its rows back to the data source from which it originally got the rows. A rowset that remains connected to its data source does not need to use a reader and writer because it can simply operate on the data source directly.
    • RowSetInternal
      By implementing the RowSetInternal interface, a RowSet object gets access to its internal state and is able to call on its reader and writer. A rowset keeps track of the values in its current rows and of the values that immediately preceded the current ones, referred to as the original values. A rowset also keeps track of (1) the parameters that have been set for its command and (2) the connection that was passed to it, if any. A rowset uses the RowSetInternal methods behind the scenes to get access to this information. An application does not normally invoke these methods directly.
    • RowSetReader
      A disconnected RowSet object that has implemented the RowSetInternal interface can call on its reader (the RowSetReader object associated with it) to populate it with data. When an application calls the RowSet.execute method, that method calls on the rowset's reader to do much of the work. Implementations can vary widely, but generally a reader makes a connection to the data source, reads data from the data source and populates the rowset with it, and closes the connection. A reader may also update theRowSetMetaData object for its rowset. The rowset's internal state is also updated, either by the reader or directly by the method RowSet.execute.
    • RowSetWriter
      A disconnected RowSet object that has implemented the RowSetInternal interface can call on its writer (the RowSetWriter object associated with it) to write changes back to the underlying data source. Implementations may vary widely, but generally, a writer will do the following:
      • Make a connection to the data source
      • Check to see whether there is a conflict, that is, whether a value that has been changed in the rowset has also been changed in the data source
      • Write the new values to the data source if there is no conflict
      • Close the connection

The RowSet interface may be implemented in any number of ways, and anyone may write an implementation. Developers are encouraged to use their imaginations in coming up with new ways to use rowsets.

IMPORTANT NOTE: Code that uses API marked "Since 1.6" must be run using a JDBC technology driver that implements the JDBC 4.0 API. You must check your driver documentation to be sure that it implements the particular features you want to use.

JDBC API Description的更多相关文章

  1. JavaWeb学习之JDBC API中常用的接口和类

    JDBC API中包含四个常用的接口和一个类分别是: 1.Connection接口 2.Statement接口 3.PreparedStatement接口 4.ResultSet接口 5.Driver ...

  2. JDBC API 事务的实践

    使用了持久化框架几乎没有使用过原生的jdbc API ,发现原来使用jdbc API来实现事务也是很简单的. 数据库的链接connection具有一个属性autocommit,这个属性默认是true, ...

  3. JDBC API访问数据库的基本步骤。

    JDBC本质:官方定义了一套操作所有关系型数据库的规则(接口),各个数据库厂商实现这个接口,提供数据库驱动jar包. 我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类. 任 ...

  4. Spring中的JDBC API

    1 JdbcTemplate的诞生 JDBC作为Java平台访问关系数据库的标准API,其成功是有目共睹的.为了避免在JDBC API在使用中的种种尴尬局面(几乎程式一样的代码,繁琐的异常处理),Sp ...

  5. JDBC API阐述

    JDBC API JDBC API 是一系列的接口,它使得应用程序能够进行数据库联接,执行SQL语句,并且得到返回结果. Driver 接口 Java.sql.Driver 接口是所有 JDBC 驱动 ...

  6. JDBC API 可滚动可编辑的结果集

    JDBC的API中的链接数据和创建statement并且执行读取ResultSet大家已经很熟悉了,这边介绍设置statement的属性使结果集可以移动并且进行编辑同步回数据库. Statement ...

  7. JDBC Api详解

    一.什么是JDBC JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Jav ...

  8. 数据库程序接口——JDBC——API解读第一篇——建立连接的核心对象

    结构图 核心对象 Driver Java通过Driver接口表示驱动,每种类型的数据库通过实现Driver接口提供自己的Driver实现类. Driver由属性,操作,事件三部分组成. 属性 公共属性 ...

  9. JDBC API浅析

    使用java开发数据库应用程序一般都需要用到四个接口:Driver.Connection.Statement.ResultSet 1.Driver接口用于加载驱动程序 2.Connection接口用于 ...

随机推荐

  1. 【JavaWeb】MVC案例之新闻列表

    MVC案例之新闻列表 作者:白宁超 2016年6月6日15:26:30 摘要:本文主要针对javaweb基本开发之MVC案例的简单操作,里面涉及mysql数据库及表的创建,以及jsp页面和servle ...

  2. PJAX初体验(主要是利用HTML5 新增API pushState和replaceState+AJAX)

    说在前面 什么是PJAX呢? 站在应用角度的就是既实现了页面无刷新的效果,同时也产生了浏览器的前进和后退,而且url也会变化. 也不是什么新鲜技术,主要是AJAX+html5 pushState和re ...

  3. Linux打包与压缩及tar命令详解

    打包和压缩   在linux中,打包和压缩可以说是两个不同的概念,弄清这两个概念对于我们理解复杂的文件后缀有非常大的帮助 打包 将若干个文件和目录打包在一起变成一个大的文件,这时只是简单的打包,所以一 ...

  4. 修复DapperExtension做Insert对象主键为Guid时不能赋值的问题

    最新的dapperExtension有个bug,就是当做Insert操作的时候,实体的主键类型为GUID的时候,会自动生产一个新的GUID替换原来的GUID,使得使用者在Insert的时候不能在外部指 ...

  5. 从Insider计划看Win10的发展

    Windows 10 Insider计划是微软为了更好的倾听用户的需求而推出的用户测试项目,参与该项目的 Insider可以免费使用Windows 10 预览版.同时这些用户还需要对 Windows ...

  6. VS2013设置护眼背景颜色

    打开VS2013 —> 工具 —> 选项 —> 环境 —> 字体和颜色 —> 选择显示项中的纯文本 —> 项目背景 —> 自定义—> 色调位85.饱和度 ...

  7. SQL游标(cursor)详细说明及内部循环使用示例

    游标 游标(cursor)是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果.每个游标区都有一个名字,用户可以用SQL语句逐一从游标中获取记录,并赋给主变量,交由主语言进一步处理. 游标是处理 ...

  8. 一元多项式的乘法与加法运算(C语言)

    输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. 输出格式: 输出分2行,分别以指数递降方 ...

  9. 工业串口和网络软件通讯平台(SuperIO 2.0)发布

    下载:SuperIO 2.0(开发手册.Demo.组件包) 官网:进入 交流群:54256083 SuperIO 2.0版本正式发布.把SCL正式更改名称为SuperIO. 一.此次升级主要包括两个方 ...

  10. SVG简介

    最近遇到SVG这个名词,于是查阅资料,做个笔记. 前言 图片的数字化.将图片存储为数据有两种方案. 位图.也被称为光栅图.即是以自然的光学的眼光将图片看成在平面上密集排布的点的集合.每个点发出的光有独 ...