其它章节的翻译参见:
http://www.pgsqldb.org/twiki/bin/view/PgSQL/JDBC4%E8%AF%B4%E6%98%8E%E4%B9%A6
=================================================================
序言
这个文档巩固了下面几个规范的内容:
“JDBC: Java的SQL类库”
“JDBC 2.1 类库”
“JDBC 2.0 标准扩展类库”
“JDBC 3.0 规范”
文档介绍了JDBC 4.0 类库的新特征和一些增强的特征。文档根据不同特征划分章节,这样可以更方便了解JDBC 4.0和3.0的不同之处。
读者如果想了解JDBC类库的详细信息,可以参考JDBC的类库文档(Javadoc),该文档可以由以下链接获得:
http://java.sun.com/products/jdbc
==========================================================================
介绍
1.1 JDBC类库
JDBC类库提供了通过Java语言访问关系数据库的能力。Java程序通过JDBC可以执行SQL语句,对获取的数据进行处理,并将变化了的数据存回数据库。在分布式异构环境中,我们还可以通过JDBC操纵多个数据源。
JDBC类库基于X/Open SQL通用语言接口(CLI),ODBC也基于此。JDBC提供了一个使用方便的映射方案:从Java语言到X/Open 通用语言接口和SQL标准的抽象实体及概念的映射。
自1997年的产生至今,JDBC类库已被广泛的接受和实现。类库的灵活性使得它可以有很多个具体实现。
1.2 平台
JDBC类库是Java平台的一部分。4.0版分为两个包:java.sql 和 javax.sql。这两个包都包含在JSE(Java桌面版)和JEE(Java企业版)中。
1.3 适合读者
该文档主要面向下列产品的开发者:
1. JDBC驱动程序
2. 基于数据库驱动提供三层架构服务的应用服务器
3. 使用JDBC类库来提供服务的工具
该文档想达到以下目的:
1.对使用JDBC类库的开发者的一个介绍
2.作为开发基于JDBC的类库的起始点
===============================================================
目标
2.1 历史
2.2 JDBC 4.0 之目标
---1.与JEE和JSE平台相处融洽
---2.与SQL:2003一致
---3.巩固前述四个规范
---4.提供中立于厂商的通用功能
---5.主要只针对SQL类型的数据源
---6.提供高层类库和工具的基础设施
---7.保持简单
---8.加强可靠性、可用性和可伸缩性
---9.保持向后兼容性
---10.与JDBC行集(RowSet)紧密联系
---11.与连接器架构(Connectors)兼容
---12.清晰描述JDBC需求
==================================================================
新特性概览
3.1 变更概览
JDBC 4.0 类库在以下领域注入了变化和新的元素:
1.自动加载java.sql.Driver
DriverManager.getConnection方法被修改以利用JSE的服务提供者机制来自动加载JDBC驱动。这样就不需要调用Class.forName方法了。
2.类库使用更容易
添加了标准JDBC注释。支持数据集(DataSet),使Java应用调用SQL数据源更加方便。
3.ROWID数据类型
添加了java.sql.RowID数据类型,使得JDBC程序可以访问SQL ROWID。
4.支持本地字符集转换(National Character Set Conversion)
添加了一些JDBC类型:NCHAR, NVARCHAR, LONGVARCHAR, NCLOB。对应的方法setNString, setNCharacterStream, setNClob也被添加到PreparedStatement接口中。
5.增强了对BLOB和CLOB的支持
Connection接口添加了生产BLOB,CLOB和NCLOB对象的方法。PreparedStatement接口添加了通过InputStream插入BLOB的方法和使用Reader插入CLOB,NCLOB的方法。Blob, Clob和NClob现在可以通过free方法释放资源。
6.SQL/XML和XML支持
SQL2003引入了用SQL表达XML数据的概念。一些类库被添加进来以支持应用对这些数据的访问。
7.包装器(Wrapper)模式
添加了解包JDBC实现的能力,使开发者可以利用在厂商实现中提供的非标准JDBC方法。
8.加强的SQLException
添加对JSE链式异常的支持。SQLException现在支持Iterable接口,所以我们可以在for-each循环里读取SQLExceptions。新添加了两类SQL异常:SQLTransientException和SQLNonTransientException。每个类都提供映射到普通SQLState类型值(译者注:SQLState?)的子类。
9.连接管理
Connection和Statement接口得到了增强,以利于对连接状态的跟踪,并增加在池环境中管理Statement对象的灵活性。
10.JDBC类库的变化
下面的JDBC接口被修改:
---10.1 Connection
添加了下列方法:createBlob, createClob, createNClob, createQueryObject, isValid, createXML, getClientInfo, setClientInfo。
---10.2 CallableStatement
添加了下列方法:getRowId, setRowId, getNClob, setNString, setNCharacterStream, setNClob, getSQLXML, setSQLXML。重载了setClob和setBlob方法。
---10.3 DatabaseMetaData
添加了下列方法:getRowIdLifetime, autoCommitFailureClosesAllResultSets, providesQueryObjectGenerator, getClientInfoProperties, supportsStoredFunctionsUsingCallSyntax. 重载了getSchemas方法。
---10.4 PreparedStatement
添加了下列方法:setRowId, setNString, setNCharacterStream, setSQLXML, isPoolable, setPoolable, setNClob。重载了setClob和setBlob方法。
---10.5 ResultSet
添加了下列方法:getHoldability, getRowId, updateRowID, getNClob, isClosed, updateNString, getSQLXML, updateSQLXML, updateNClob。
---10.6 Statement
添加了isClosed和getResultSetHoldability方法。
---10.7 DataSource
添加了createQueryObject方法。
---10.8 PooledConnection
添加了addStatementEventListener和removeStatementEventListener方法。
====================================================================
概览
JDBC类库使得Java程序可以访问多个数据源,但在大多数情况下,这个数据源是关系数据库,并且通过SQL访问。然而,实现JDBC技术的驱动也可以基于其它的数据源,包括遗留文件系统和面向对象的系统。
JDBC类库的主要目的就是提供应用程序访问多种数据源的标准接口。
这一章介绍JDBC类库的一些关键概念,并描述JDBC应用的两个通用环境及其中的功能实现。
4.1 创建连接
JDBC类库中的Connection接口代表了底层数据源的一个连接。
在典型场景中,JDBC应用程序使用两种机制连接到数据源:
1.DriverManager --- 这个类在JDBC 1.0中引入,它使用硬编码的URL来加载驱动。
2.DataSource --- 这个接口在JDBC 2.0可选包中引入。它优于DriverManager方式,因为它隐藏了数据源的详细信息。我们通过设置DataSource的属性来标明它代表的数据源。当getConnection方法被调用时,DataSource对象会返回一个对应的连接。我们可以通过改变DataSource的属性来使它指向另一个数据源,而不是改变程序代码。而且,如果DataSource的实现改变了,使用它的应用程序代码不需要改变。
JDBC类库也定义了两个DataSource的扩展,用来支持企业级应用,如下:
1.ConnectionPoolDataSource --- 支持物理连接的缓存和重用,这样可以提高应用的性能和可伸缩性。
2.XADataSource --- 提供可以使用在分布式事务中的连接。
Chapter14 - END
14.批更新(Batch Update)---14.1 批更新概述-------14.1.1 Statements-------14.1.2 成功的执行-------14.1.3 在执行过程中处理失败-------14.1.4 PreparedStatement对象-------14.1.5 CallableStatement对象15.结果集(Result Sets)---15.1 结果集分类-------15.1.1 结果集类型-------15.1.2 结果集并发度-------15.1.3 结果集持久性(ResultSet Holdability)-------------15.1.3.1 决定结果集持久性-------15.1.4 设置结果集的类型、并发度和持久性---15.2 创建和操纵结果集-------15.2.1 创建结果集对象-------15.2.2 移动游标-------15.2.3 取值-------------15.2.3.1 数据类型转换-------------15.2.3.2 结果集元数据-------------15.2.3.3 取NULL值-------15.2.4 修改结果集-------------15.2.4.1 更新行-------------15.2.4.2 删除行-------------15.2.4.3 插入行-------------15.2.4.4 给定位置的更新和删除-------15.2.5 关闭结果集16.高级数据类型---16.1 SQL数据类型分类---16.2 SQL2003类型映射---16.3 Blob和Clob对象-------16.3.1 创建Blob和Clob对象-------16.3.2 在结果集中取BLOB和CLOB类型值-------16.3.3 访问Blob和Clob对象数据-------16.3.4 保存Blob和Clob对象-------16.3.5 更改Blob和Clob对象-------16.3.6 释放Blob和Clob资源---16.4 Array对象-------16.4.1 获取Array对象-------16.4.2 保存Array对象-------16.4.3 更新Array对象---16.5 Ref对象-------16.5.1 取REF值-------16.5.2 获取Ref引用的对象-------16.5.3 保存Ref对象-------16.5.4 保存Ref引用的对象-------16.5.5 元数据---16.6 DISTINCT类型-------16.6.1 获取DISTINCT类型-------16.6.2 保存DISTINCT类型-------16.6.3 元数据---16.7 结构化类型-------16.7.1 获取结构化类型-------16.7.2 保存结构化类型-------16.7.3 元数据---16.8 数据链接-------16.8.1 获取外部数据的引用-------16.8.2 保存外部数据的引用-------16.8.3 元数据17.定制类型映射---17.1 类型映射---17.2 类转换---17.3 SQL数据流-------17.3.1 取数据-------17.3.2 保存数据---17.4 示例-------17.4.1 一个SQL结构化类型-------17.4.2 SQLData的实现-------17.4.3 SQL类型继承在Java语言中的镜像-------17.4.4 SQL DISTINCT类型映射示例---17.5 转换组(Transform Groups)的作用---17.6 映射的普遍性---17.7 NULL数据18.和连接器(Connectors)的关系---18.1 系统约定---18.2 映射连接器系统约定到JDBC接口---18.3 用连接器的RAR文件格式将JDBC驱动打包19. SQL ROWID---19.1 RowId合理(Validity)期限---19.2 取RowId值---19.3 使用RowId20.新添的开发利器---20.1 概览-------20.1.1 JDBC注释(Annotations)-------20.1.2 Query接口-------20.1.3 BaseQuery接口-------20.1.4 DataSet接口-------20.1.5 自定义类-------20.1.6 QueryObjectGenerator接口---20.2 创建实现Query接口的实例-------20.2.1 创建一个Query接口-------20.2.2 Query接口的具体实现-------------20.2.2.1 Connection.createQueryObject和DataSource.createQueryObject方法-------------20.2.2.2 调用Query接口的方法-------------20.2.2.3 关闭Query对象---20.3 JDBC注释(Annotations)-------20.3.1 Query注释-------------20.3.1.1 sql注释成员-------------20.3.1.2 readOnly注释成员-------------20.3.1.3 connected注释成员-------------20.3.1.4 allColumnsMapped注释成员-------------20.3.1.5 scrollable注释成员-------------20.3.1.6 参数化的sql注释成员-------20.3.2 Update注释-------------20.3.2.1 sql注释成员-------------20.3.2.2 keys注释成员-------------20.3.2.3 返回影响行数-------------20.3.2.4 参数化的sql注释成员-------20.3.3 Column注释-------------20.3.3.1 name注释成员-------------20.3.3.2 uniqueIdentifier注释成员-------20.3.4 AutoGeneratedKeys注释-------20.3.5 Table注释-------------20.3.5.1 name注释成员---20.4 数据集(DataSet)参数化类型-------20.4.1 创建数据集参数化类型实例-------20.4.2 操纵数据集-------------20.4.2.1 查看数据集-------------20.4.2.2 将数据集转换成行集(RowSet)-------------20.4.2.3 创建当前行的一个拷贝-------------20.4.2.4 插入行-------------20.4.2.5 删除行-------------20.4.2.6 修改行-------------20.4.2.7 关闭数据集对象-------------20.4.2.8 同步数据集对象---20.5 QueryObjectFactory类-------20.5.1 QueryObjectFactory的方法-------------20.5.1.1 createDefaultQueryObject方法-------------20.5.1.2 createQueryObject方法-------20.5.2 管理事务21.SQL2003和XML22.包装器(Wrapper)附录A.修正历史附录B.数据类型转换表附录C.标量方法(Scalar Functions)------C.1 数字方法------C.2 字符串方法------C.3 时间和日期方法------C.4 系统方法------C.5 转换方法附录D.JDBC注释------D.1 AutogeneratedKeys(自动生成关键字)注释------D.2 Query(查询)注释------D.3 Update(更新)注释------D.4 Column(列)注释------D.5 Table(表)注释附录E.相关文档
英文原文:14. Batch Updates 11714.1 Description of Batch Updates 11714.1.1 Statements 11714.1.2 Successful Execution 11814.1.3 Handling Failures during Execution 11914.1.4 PreparedStatement Objects 12014.1.5 CallableStatement Objects 121
15. Result Sets 12315.1 Kinds of ResultSet Objects 12315.1.1 ResultSet Types 12315.1.2 ResultSet Concurrency 12415.1.3 ResultSet Holdability 12515.1.3.1 Determining ResultSet Holdability 12515.1.4 Specifying ResultSet Type, Concurrency and Holdability 12615.2 Creating and Manipulating ResultSet Objects 12615.2.1 Creating ResultSet Objects 12615.2.2 Cursor Movement 12715.2.3 Retrieving Values 12815.2.3.1 Data Type Conversions 12915.2.3.2 ResultSet Metadata 12915.2.3.3 Retrieving NULL values 12915.2.4 Modifying ResultSet Objects 12915.2.4.1 Updating a Row 13015.2.4.2 Deleting a Row 13115.2.4.3 Inserting a Row 13215.2.4.4 Positioned Updates and Deletes 13315.2.5 Closing a ResultSet Object 134
16. Advanced Data Types 13516.1 Taxonomy of SQL Types 13516.2 Mapping of SQL2003 Types 13716.3 Blob and Clob Objects 13716.3.1 Creating Blob and Clob Objects 13716.3.2 Retrieving BLOB and CLOB Values in a ResultSet 13816.3.3 Accessing Blob and Clob Object Data 13816.3.4 Storing Blob and Clob Objects 13916.3.5 Altering Blob and Clob Objects 14016.3.6 Releasing Blob and Clob Resources 14016.4 Array Objects 14116.4.1 Retrieving Array Objects 14116.4.2 Storing Array Objects 14116.4.3 Updating Array Objects 14216.5 Ref Objects 14216.5.1 Retrieving REF Values 14216.5.2 Retrieving the Referenced Value 14316.5.3 Storing Ref Objects 14316.5.4 Storing the Referenced Value 14316.5.5 Metadata 14416.6 Distinct Types 14416.6.1 Retrieving Distinct Types 14416.6.2 Storing Distinct Types 14516.6.3 Metadata 14516.7 Structured Types 14616.7.1 Retrieving Structured Types 14616.7.2 Storing Structured Types 14616.7.3 Metadata 14716.8 Datalinks 14716.8.1 Retrieving References to External Data 14816.8.2 Storing References to External Data 14816.8.3 Metadata 148
17. Customized Type Mapping 14917.1 The Type Mapping 14917.2 Class Conventions 15017.3 Streams of SQL Data 15117.3.1 Retrieving Data 15117.3.2 Storing Data 15217.4 Examples 15317.4.1 An SQL Structured Type 15317.4.2 SQLData Implementations 15517.4.3 Mirroring SQL Inheritance in the Java Programming Language 15917.4.4 Example Mapping of SQL DISTINCT Type 16017.5 Effect of Transform Groups 16117.6 Generality of the Approach 16217.7 NULL Data 162
18. Relationship to Connectors 16518.1 System Contracts 16518.2 Mapping Connector System Contracts to JDBC Interfaces 16618.3 Packaging JDBC Drivers in Connector RAR File Format 167
19. SQL ROWID 16919.1 Lifetime of RowId Validity 16919.2 Retrieving RowId Values 17019.3 Using RowId Values 170
20. Ease of Development 17320.1 Overview 17320.1.1 JDBC Annotations 17320.1.2 Query Interface 17420.1.3 BaseQuery Interface 17420.1.4 DataSet interface 17420.1.5 User-Defined Class 17520.1.6 QueryObjectGenerator Interface 17620.2 Creating an instance of a Query interface 17620.2.1 Creating a Query Interface 17620.2.2 Concrete Query Interface implementation 17720.2.2.1 Connection.createQueryObject and DataSource.createQueryObject methods 17720.2.2.2 Invoking Query Interface Methods 17820.2.2.3 Closing a Query Object 17820.3 JDBC Annotations 17820.3.1 Query Annotation 17920.3.1.1 sql annotation element 17920.3.1.2 readOnly annotation element 18020.3.1.3 connected annotation element 18020.3.1.4 allColumnsMapped annotation element 18020.3.1.5 scrollable annotation element 18220.3.1.6 Parameterized sql Annotation element 18220.3.2 Update Annotation 18320.3.2.1 sql annotation element 18320.3.2.2 keys annotation element 18420.3.2.3 Returning an Update Count 18420.3.2.4 Parameterized sql Annotation element 18420.3.3 Column Annotation 18520.3.3.1 name annotation element 18520.3.3.2 uniqueIdentifier annotation element 18620.3.4 AutoGeneratedKeys annotation 18720.3.5 Table Annotation 18820.3.5.1 name annotation element 18820.4 DataSet Parameterized Type 18920.4.1 Creating a DataSet Parameterized Type Instance 19020.4.2 Manipulating a DataSet 19020.4.2.1 Traversing a DataSet 19120.4.2.2 Converting a DataSet to a RowSet 19120.4.2.3 Creating a copy of the Current Row 19120.4.2.4 Inserting Rows 19120.4.2.5 Deleting Rows 19320.4.2.6 Modifying Rows 19320.4.2.7 Closing a DataSet Object 19420.4.2.8 Synchronizing a DataSet 19420.5 QueryObjectFactory Class 19520.5.1 QueryObjectFactory Methods 19520.5.1.1 createDefaultQueryObject method 19520.5.1.2 createQueryObject method 19620.5.2 Managing Transactions 196
21. SQL:2003 and XML 197
22. Wrapper 199
A. Revision History 201B. Data Type Conversion Tables 203C. Scalar Functions 211C.1 NUMERIC FUNCTIONS 211C.2 STRING FUNCTIONS 212C.3 TIME and DATE FUNCTIONS 213C.4 SYSTEM FUNCTIONS 213C.5 CONVERSION FUNCTIONS 214D. JDBC Annotations 215D.1 AutogeneratedKeys Annotation 215D.2 Query Annotation 215D.3 Update Annoation 216D.4 Column Annotation 216D.5 Table Annotation 216E. Related Documents 217
欢迎提出修改意见
Chapter9 - Chapter139.事务---9.1 事务边界和自动提交(Auto-commit)------9.1.1 禁用自动提交模式---9.2 事务隔离级别------9.2.1 使用setTransactionIsolation方法------9.2.2 性能考虑---9.3 保存点(Savepoints)------9.3.1 设置和回滚保存点------9.3.2 释放(release)保存点10.连接---10.1 驱动程序分类---10.2 Driver接口-------10.2.1 加载一个实现了java.sql.Driver接口的驱动---10.3 DriverManager类-------10.3.1 SQLPermission类---10.4 DataSource接口-------10.4.1 DataSource属性-------10.4.2 JNDI类库和应用移植能力(Application Portability)-------10.4.3 用DataSource对象获得连接11.连接池---11.1 ConnectionPoolDataSource类和PooledConnection类---11.2 连接事件---11.3 三层架构中的连接池---11.4 DataSource的实现和连接池---11.5 部署---11.6 用池连接(Pooled Connections)实现语句(Statement)重用-------11.6.1 使用池语句(Pooled Statement)-------11.6.2 关闭池语句---11.7 ConnectionPoolDataSource类的属性12.分布式事务---12.1 基础设施---12.2 XADataSource和XAConnection接口-------12.2.1 部署一个XADataSource对象-------12.2.2 获得一个连接---12.3 XAResource接口---12.4 事务管理-------12.4.1 两阶段提交---12.5 关闭连接---12.6 XAResource接口的局限13.语句(Statements)---13.1 Statement接口-------13.1.1 创建Statements-------------13.1.1.1 设置ResultSet的相关特征-------13.1.2 执行Statement-------------13.1.2.1 返回ResultSet对象-------------13.1.2.2 返回影响行数(Update Count)-------------13.1.2.3 使用execute方法-------13.1.3 关闭Statement---13.2 PreparedStatement接口-------13.2.1 创建PreparedStatement对象-------------13.2.1.1 设置ResultSet的相关特征-------13.2.2 参数设置-------------13.2.2.1 类型转换-------------13.2.2.2 本地字符集转换(National Character Set Conversions)-------------13.2.2.3 使用setObject方法进行类型转换-------------13.2.2.4 设置NULL参数-------13.2.3 描述PreparedStatement的输出和输入-------13.2.4 执行PreparedStatement-------------13.2.4.1 返回ResultSet对象-------------13.2.4.2 返回影响行数-------------13.2.4.3 使用execute方法---13.3 CallableStatement接口-------13.3.1 创建CallableStatement对象-------13.3.2 参数设置-------------13.3.2.1 IN参数-------------13.3.2.2 OUT参数-------------13.3.2.3 INOUT参数-------13.3.3 执行CallableStatement-------------13.3.3.1 返回单个ResultSet对象-------------13.3.3.2 返回影响行数-------------13.3.3.3 返回Unknown或多个结果---13.4 转义语法(Escape Syntax)-------13.4.1 标量方法(Scalar Functions)-------13.4.2 日期和时间字符串-------13.4.3 外联结(Outer Joins)-------13.4.4 存储过程-------13.4.5 LIKE转义符---13.5 性能提示(Hints)---13.6 获取由数据库自动生成的关键字英文原文:9. Transactions 539.1 Transaction Boundaries and Auto-commit 539.1.1 Disabling Auto-commit Mode 549.2 Transaction Isolation Levels 559.2.1 Using the setTransactionIsolation Method 569.2.2 Performance Considerations 569.3 Savepoints 579.3.1 Setting and Rolling Back to a Savepoint 579.3.2 Releasing a Savepoint 58
10. Connections 5910.1 Types of Drivers 6010.2 The Driver Interface 6010.2.1 Loading a driver that implements java.sql.Driver 6110.3 The DriverManager Class 6110.3.1 The SQLPermission Class 6310.4 The DataSource Interface 6310.4.1 DataSource Properties 6410.4.2 The JNDI API and Application Portability 6510.4.3 Getting a Connection with a DataSource Object 66
11. Connection Pooling 6711.1 ConnectionPoolDataSource and PooledConnection 6911.2 Connection Events 7011.3 Connection Pooling in a Three-tier Environment 7111.4 DataSource Implementations and Connection Pooling 7211.5 Deployment 7411.6 Reuse of Statements by Pooled Connections 7511.6.1 Using a Pooled Statement 7611.6.2 Closing a Pooled Statement 7711.7 ConnectionPoolDataSource Properties 78
12. Distributed Transactions 8112.1 Infrastructure 8112.2 XADataSource and XAConnection 8412.2.1 Deploying an XADataSource Object 8512.2.2 Getting a Connection 8612.3 XAResource 8612.4 Transaction Management 8712.4.1 Two-phase Commit 8812.5 Closing the Connection 9012.6 Limitations of the XAResource Interface 90
13. Statements 9313.1 The Statement Interface 9313.1.1 Creating Statements 9313.1.1.1 Setting ResultSet Characteristics 9413.1.2 Executing Statement Objects 9413.1.2.1 Returning a ResultSet object 9513.1.2.2 Returning an Update Count 9513.1.2.3 Using the Method execute 9513.1.3 Closing Statement Objects 9613.2 The PreparedStatement Interface 9713.2.1 Creating a PreparedStatement Object 9713.2.1.1 Setting ResultSet Characteristics 9713.2.2 Setting Parameters 9813.2.2.1 Type Conversions 9813.2.2.2 National Character Set Conversions 9913.2.2.3 Type Conversions Using the Method setObject 9913.2.2.4 Setting NULL Parameters 10013.2.3 Describing Outputs and Inputs of a PreparedStatementObject 10113.2.4 Executing a PreparedStatement Object 10213.2.4.1 Returning a ResultSet Object 10213.2.4.2 Returning a Row Count 10213.2.4.3 Using the Method execute 10313.3 The CallableStatement Interface 10313.3.1 Creating a CallableStatement Object 10413.3.2 Setting Parameters 10413.3.2.1 IN Parameters 10513.3.2.2 OUT Parameters 10513.3.2.3 INOUT Parameters 10613.3.3 Executing a CallableStatement Object 10613.3.3.1 Returning a Single ResultSet Object 10713.3.3.2 Returning a Row Count 10713.3.3.3 Returning Unknown or Multiple Results 10713.4 Escape Syntax 10913.4.1 Scalar Functions 11013.4.2 Date and Time Literals 11113.4.3 Outer Joins 11113.4.4 Stored Procedures 11213.4.5 LIKE Escape Characters 11313.5 Performance Hints 11313.6 Retrieving Auto Generated Keys 113欢迎提出修改意见
/*Modified in 2005/9/27 by Andyluo."简介" change to "序言""目标听众" change to "适合读者""兼容性" change to "服从规则"*/序言 *排版格式提交反馈
1.介绍---1.1 JDBC 类库---1.2 平台---1.3 适合读者 *---1.4 鸣谢2.目标---2.1 历史---2.2 目标概要3.新特性概览---3.1 变动概要4.概要---4.1 建立连接---4.2 执行SQL语句和操纵结果------4.2.1 对高级SQL数据类型的支持---4.3 两层模型---4.4 三层模型---4.5 J2EE中的JDBC5.类和接口---5.1 java.sql包---5.2 javax.sql包6.服从规则(Compliance) *---6.1 定义---6.2 指导原则和要求---6.3 JDBC 1.0 API的服从规则---6.4 JDBC 2.0 API的服从规则---6.5 JDBC 3.0 API的服从规则---6.6 JDBC 4.0 API的服从规则---6.7 决定服从级别---6.8 不赞成使用的API7.数据库元数据(Database Metadata)---7.1 创建DatabaseMetadata对象---7.2 获取概要(general)信息---7.3 决定要支持的特性---7.4 对数据源的限制---7.5 SQL对象及其属性---7.6 对事务的支持---7.7 新加入的方法---7.8 经过修改的方法8.异常---8.1 SQLException类------8.1.1 对JSE中链式异常的支持------8.1.2 察看SQLException-----------8.1.2.1 使用ForEach循环体察看SQLException---8.2 SQLWarning类---8.3 DataTruncation类------8.3.1 不出声地截断(Silent Truncation)---8.4 BatchUpdateException类---8.5 SQL异常分类------8.5.1 非暂态SQL异常------8.5.2 暂态SQL异常英文原文:Preface 1Typographic Conventions 2Submitting Feedback 2
1. Introduction 31.1 The JDBC API 31.2 Platforms 31.3 Target Audience 41.4 Acknowledgements 4
2. Goals 72.1 History 72.2 Overview of Goals 7
3. Summary of New Features 113.1 Overview of changes 11
4. Overview 154.1 Establishing a Connection 154.2 Executing SQL Statements and Manipulating Results 164.2.1 Support for SQL Advanced Data Types 174.3 Two-tier Model 174.4 Three-tier Model 184.5 JDBC in the J2EE Platform 20
5. Classes and Interfaces 215.1 The java.sql Package 215.2 The javax.sql Package 26
6. Compliance 316.1 Definitions 316.2 Guidelines and Requirements 326.3 JDBC 1.0 API Compliance 336.4 JDBC 2.0 API Compliance 336.5 JDBC 3.0 API Compliance 346.6 JDBC 4.0 API Compliance 356.7 Determining Compliance Level 356.8 Deprecated APIs 36
7. Database Metadata 397.1 Creating a DatabaseMetadata Object 407.2 Retrieving General Information 407.3 Determining Feature Support 417.4 Data Source Limits 417.5 SQL Objects and Their Attributes 427.6 Transaction Support 427.7 New Methods 427.8 Modified Methods 43
8. Exceptions 458.1 SQLException 458.1.1 Support for the J2SE Chained Execeptions 468.1.2 Navigating SQLExceptions 468.1.2.1 Using a For-Each Loop with SQLExceptions 478.2 SQLWarning 488.3 DataTruncation 488.3.1 Silent Truncation 498.4 BatchUpdateException 498.5 Categorized SQLExceptions 508.5.1 NonTransient SQLExceptions 508.5.2 Transient SQLExceptions 51欢迎提出修改意见!
JSR 221Early DraftJune 02, 2005
JDBC是一个包含在JSE(Java桌面版)和JEE(Java企业版)里的API,它提供几乎所有SQL数据库之间的跨DBMS(数据库管理系统)连接,和对报表和文本文件等表格数据源(tabular data sources)的访问。通过一个JDBC的驱动程序,我们能聚集处在不同环境中(heterogeneous environment)的企业数据。
JDBC technology is an API (included in both J2SE and J2EE releases) that provides cross-DBMS connectivity to a wide range of SQL databases and access to other tabular data sources, such as spreadsheets or flat files. With a JDBC technology-enabled driver, you can connect all corporate data even in a heterogeneous environment.
历史
往事如风
函数内定义的局部变量的栈内存在函数结束后就会被释放,
但用{}隔开的作用域里定义的局部变量不会在离开作用域时马上释放掉
(见《高质量C++编程指南》P51,7.7杜绝野指针)
<My Opinion: 局部变量的内存释放发生于函数结束,与作用域无关>
将CString类型的变量转化成char*:
char* keyChar = key.GetBuffer(0); //key为CString类型
...
key.ReleaseBuffer(); //用完CString后记得释放缓冲
刚开始用C++,以前都是用的Java,希望Everything will go pretty well!
关键词: HSQL DB, Eclipse 3.1, Apache Commons, Swing Hacks
16:59 2005-7-23
在HSQL DB里,一个Database由四个文件组成:(如数据库test)
test.properties
test.script
test.log
test.data
test.backup
propertiest文件定义数据库的基本设置;
script包含数据库表和数据库对象的定义,还有non-cached tables的数据;
log包含最近对数据库进行的操作;
data包含被缓存表(cached tables)
HSqlDB的JDBC方法分为:
连接相关的方法
元数据方法
数据库访问方法
用Hsql建立数据库:
在一个项目里执行建立数据库的代码,生成的数据库在Workspace对应的项目文件夹下;
而在一个项目里调用另一个项目的生成数据库的方法,生成的数据库在Eclipse Home文件夹下
Indicate things are happening in the view –only through jobs
***getSite().getAdapter(IWorkbenchSiteProgressService.class)***
Eclipse3.1的一些新特性:
Open Untitled File
A text editor can be opened without creating a file first.
Simply go to File > New > Untitled Text File.
Text编辑器也开始支持超链接了,按住Ctrl键,将鼠标放在URL的上面就可以了
我以前写过一篇制作和使用库插件的文章,制作库插件得花一点时间,但在3.1里做就非常方便了,
可以直接将第三方库转换为库插件:
Create a plug-in from existing JAR
PDE now provides a wizard that creates a plug-in from existing JAR archives.
This wizard is ideal if you would like to package third-party non-Eclipse JARs as an
Eclipse plug-in.
The wizard can be invoked via File > New > Project > Plug-in from existing JAR archives.
Java SE6(Mustang)要提供中文的Doc了,尽管用英文的Doc已经习惯了,但对于大多数程序员来说
也是一件好事啊。呵呵(见blog图Excellence)
18:03 2005-8-3
Apache Commons Lang中的一些方法
首字母大写打印字符串:StringUtils.capitalize(string);
打印布尔值,用Yes或No表示:BooleanUtils.toStringYesNo(true);
求一个数值数组中的最大值: NumberUtils.max(array);
打印数组:ArrayUtils.toString(array);
测试数值是否在某一范围:
Range range = new DoubleRange(90,120);
range.containsDouble(102.5);
随机数:RandomUtils.nextInt(100);
测试字符串是否为数值:NumberUtils.isNumber(string/*eg. 0x3F*/);
String的split方法有时会得到意外的结果:
"aaa|bbb|ccc".split("|") 结果:{,a,a,a,}
用"aaa|bbb|ccc".split("\\|")才能得到正确结果,*和+也是如此
6:58 2005-8-6
w.k. Eclipse3.1垃圾回收后只占18M内存,我现在可以一直开着Eclipse了,呵呵
11:19 2005-8-7
找出Java反编译利器DJ,打算破解IDEA。
9:09 2005-8-8
晕,发现Eclipse的帮助不能显示了,显示找不着org.eclipse.help.internal.webapp.data包,
找到webapp的插件目录,发现里面的web-inf目录是小写的,里面lib目录包含的servlet.jar
刚好包括了org.eclipse.help.internal.webapp.data包,可以肯定是web-inf目录小写的缘故,
但为什么会变成小写的呢,我的D盘数据通过ghost从俱乐部的机器上拷过来的,以前好像听说过
一些软件不支持大小写,不出意外的话,就是ghost帮我改的咯。
16:00 2005-8-10
看一个项目的源代码时,如果出现字符方面的错误,有可能是Encoding没有设置好,
看看Readme要求怎么设(一般要设为UTF-8)
19:30 2005-8-11
运行程序和浏览URL(包括mailto:):
/** Launch default browser on Windows */
if (GlobalSettings.isWindows())
{
Program.launch(localHref);
}
19:38 2005-8-11
mailto:?body=TheEmailBody&subject=TheSubject
15:14 2005-8-12
将Shell最小化:shell.setMinimized(true);
18:19 2005-8-18
JDK5.0文档,CHM版本的:
http://www.matrix.org.cn/resource/upload/content/2005_07_30_191519_vorbArhhNZ.chm
22:47 2005-8-25
Get Large File Icons
Not to leave Windows lonely, here is an undocumented class that will let you obtain large desktop icons and other file information. The FileSystemView only provides access to file icons of a "default" size, which usually means 16 by 16 pixels. If you look at your operating system desktop, however, you may see icons that are much bigger, with more detail. There is no standardized way of getting to the larger icons, but on Windows you can use the hidden class called sun.awt.shell.ShellFolder to retrieve larger (32 by 32) desktop file icons. This class is only available in Sun's JVM, so it won't work with other vendors, or on other platforms.
The code below will take a filename and show the file's large icon in a window.
public class LargeIconTest {
public static void main(String[] args) throws Exception {
// Create a File instance of an existing file
File file = new File(args[0]);
// Get metadata and create an icon
sun.awt.shell.ShellFolder sf =
sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(sf.getIcon(true));
System.out.println("type = " + sf.getFolderType());
// show the icon
JLabel label = new JLabel(icon);
JFrame frame = new JFrame();
frame.getContentPane().add(label);
frame.pack();
frame.show();
ShellFolder is a wrapper for the metadata of the selected file. From this object, you can retrieve both the icon and a text description of the file's type. If you run this on an MP3 file, it might print the string "type = MPEG Layer 3 Audio" and show a 32 by 32 pixel iTunes MP3 icon.
Change the Look and Feel of an App from the Command Line
Sometimes, Swing applications don't provide a way to change a look and feel at runtime. When using an application like this, you can override the default look and feel with your own setting from the command line using the swing.defaultlaf property.
java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel
myapp.MainClass
You can also use this technique to apply a new look and feel that the original developer never thought of.
Hacks for the Metal Look and Feel
There are a variety of undocumented properties that control the look of Metal, Swing's cross-platform look and feel. JTree nodes can have lines connecting children to their parent, but depending on your current setup, the lines may or may not be showing. You can control the lines with a client property called JTree.lineStyle. Add this code after you create your JTree.
// show the lines in a JTree
tree.putClientProperty("JTree.lineStyle", "Angled");
// hide the lines in a jtree
tree.putClientProperty("JTree.lineStyle", "None");
One of the big complaints about Metal is that the menus and labels use bold fonts. With another quick undocumented system property, you can turn that off.
java -Dswing.boldMetal=false myapp.MainClass
You can also turn on the rollover for JToolBar buttons by using a secret property. A rollover is useful because it gives the user visual feedback that the mouse cursor is over the right place. Given the size of the typical toolbar button, this feedback is essential.
toolbar.putClientProperty("JToolBar.isRollover",Boolean.TRUE);
There is a longer (though, by no means comprehensive) list of properties on this wiki page. Swing, and Metal in particular, has lots of undocumented system properties. As you discover them, please add your own comments to the wiki page. Also remember that these are undocumented for a reason, and could easily change or go away in the future. Use them at your own risk.
Conclusion
Swing is a powerful toolkit with lots of hidden features that you can use to bring out the best in your application. This article documents just a few interesting techniques. Swing Hacks from O'Reilly covers another 100 hacks to improve your software. None of the techniques I've shown are required features, but they can add a level of polish that will make your apps stand out from the rest. And in a crowded software marketplace, anything that makes your program better than the competition is a good thing.