其它章節的翻譯參見:
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 使用RowId
20.新添的開發利器
---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和XML
22.包裝器(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 117
14.1 Description of Batch Updates 117
14.1.1 Statements 117
14.1.2 Successful Execution 118
14.1.3 Handling Failures during Execution 119
14.1.4 PreparedStatement Objects 120
14.1.5 CallableStatement Objects 121
15. Result Sets 123
15.1 Kinds of ResultSet Objects 123
15.1.1 ResultSet Types 123
15.1.2 ResultSet Concurrency 124
15.1.3 ResultSet Holdability 125
15.1.3.1 Determining ResultSet Holdability 125
15.1.4 Specifying ResultSet Type, Concurrency and Holdability 126
15.2 Creating and Manipulating ResultSet Objects 126
15.2.1 Creating ResultSet Objects 126
15.2.2 Cursor Movement 127
15.2.3 Retrieving Values 128
15.2.3.1 Data Type Conversions 129
15.2.3.2 ResultSet Metadata 129
15.2.3.3 Retrieving NULL values 129
15.2.4 Modifying ResultSet Objects 129
15.2.4.1 Updating a Row 130
15.2.4.2 Deleting a Row 131
15.2.4.3 Inserting a Row 132
15.2.4.4 Positioned Updates and Deletes 133
15.2.5 Closing a ResultSet Object 134
16. Advanced Data Types 135
16.1 Taxonomy of SQL Types 135
16.2 Mapping of SQL2003 Types 137
16.3 Blob and Clob Objects 137
16.3.1 Creating Blob and Clob Objects 137
16.3.2 Retrieving BLOB and CLOB Values in a ResultSet 138
16.3.3 Accessing Blob and Clob Object Data 138
16.3.4 Storing Blob and Clob Objects 139
16.3.5 Altering Blob and Clob Objects 140
16.3.6 Releasing Blob and Clob Resources 140
16.4 Array Objects 141
16.4.1 Retrieving Array Objects 141
16.4.2 Storing Array Objects 141
16.4.3 Updating Array Objects 142
16.5 Ref Objects 142
16.5.1 Retrieving REF Values 142
16.5.2 Retrieving the Referenced Value 143
16.5.3 Storing Ref Objects 143
16.5.4 Storing the Referenced Value 143
16.5.5 Metadata 144
16.6 Distinct Types 144
16.6.1 Retrieving Distinct Types 144
16.6.2 Storing Distinct Types 145
16.6.3 Metadata 145
16.7 Structured Types 146
16.7.1 Retrieving Structured Types 146
16.7.2 Storing Structured Types 146
16.7.3 Metadata 147
16.8 Datalinks 147
16.8.1 Retrieving References to External Data 148
16.8.2 Storing References to External Data 148
16.8.3 Metadata 148
17. Customized Type Mapping 149
17.1 The Type Mapping 149
17.2 Class Conventions 150
17.3 Streams of SQL Data 151
17.3.1 Retrieving Data 151
17.3.2 Storing Data 152
17.4 Examples 153
17.4.1 An SQL Structured Type 153
17.4.2 SQLData Implementations 155
17.4.3 Mirroring SQL Inheritance in the Java Programming Language 159
17.4.4 Example Mapping of SQL DISTINCT Type 160
17.5 Effect of Transform Groups 161
17.6 Generality of the Approach 162
17.7 NULL Data 162
18. Relationship to Connectors 165
18.1 System Contracts 165
18.2 Mapping Connector System Contracts to JDBC Interfaces 166
18.3 Packaging JDBC Drivers in Connector RAR File Format 167
19. SQL ROWID 169
19.1 Lifetime of RowId Validity 169
19.2 Retrieving RowId Values 170
19.3 Using RowId Values 170
20. Ease of Development 173
20.1 Overview 173
20.1.1 JDBC Annotations 173
20.1.2 Query Interface 174
20.1.3 BaseQuery Interface 174
20.1.4 DataSet interface 174
20.1.5 User-Defined Class 175
20.1.6 QueryObjectGenerator Interface 176
20.2 Creating an instance of a Query interface 176
20.2.1 Creating a Query Interface 176
20.2.2 Concrete Query Interface implementation 177
20.2.2.1 Connection.createQueryObject and DataSource.createQueryObject methods 177
20.2.2.2 Invoking Query Interface Methods 178
20.2.2.3 Closing a Query Object 178
20.3 JDBC Annotations 178
20.3.1 Query Annotation 179
20.3.1.1 sql annotation element 179
20.3.1.2 readOnly annotation element 180
20.3.1.3 connected annotation element 180
20.3.1.4 allColumnsMapped annotation element 180
20.3.1.5 scrollable annotation element 182
20.3.1.6 Parameterized sql Annotation element 182
20.3.2 Update Annotation 183
20.3.2.1 sql annotation element 183
20.3.2.2 keys annotation element 184
20.3.2.3 Returning an Update Count 184
20.3.2.4 Parameterized sql Annotation element 184
20.3.3 Column Annotation 185
20.3.3.1 name annotation element 185
20.3.3.2 uniqueIdentifier annotation element 186
20.3.4 AutoGeneratedKeys annotation 187
20.3.5 Table Annotation 188
20.3.5.1 name annotation element 188
20.4 DataSet Parameterized Type 189
20.4.1 Creating a DataSet Parameterized Type Instance 190
20.4.2 Manipulating a DataSet 190
20.4.2.1 Traversing a DataSet 191
20.4.2.2 Converting a DataSet to a RowSet 191
20.4.2.3 Creating a copy of the Current Row 191
20.4.2.4 Inserting Rows 191
20.4.2.5 Deleting Rows 193
20.4.2.6 Modifying Rows 193
20.4.2.7 Closing a DataSet Object 194
20.4.2.8 Synchronizing a DataSet 194
20.5 QueryObjectFactory Class 195
20.5.1 QueryObjectFactory Methods 195
20.5.1.1 createDefaultQueryObject method 195
20.5.1.2 createQueryObject method 196
20.5.2 Managing Transactions 196
21. SQL:2003 and XML 197
22. Wrapper 199
A. Revision History 201
B. Data Type Conversion Tables 203
C. Scalar Functions 211
C.1 NUMERIC FUNCTIONS 211
C.2 STRING FUNCTIONS 212
C.3 TIME and DATE FUNCTIONS 213
C.4 SYSTEM FUNCTIONS 213
C.5 CONVERSION FUNCTIONS 214
D. JDBC Annotations 215
D.1 AutogeneratedKeys Annotation 215
D.2 Query Annotation 215
D.3 Update Annoation 216
D.4 Column Annotation 216
D.5 Table Annotation 216
E. Related Documents 217
歡迎提出修改意見
Chapter9 - Chapter13
9.事務
---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 53
9.1 Transaction Boundaries and Auto-commit 53
9.1.1 Disabling Auto-commit Mode 54
9.2 Transaction Isolation Levels 55
9.2.1 Using the setTransactionIsolation Method 56
9.2.2 Performance Considerations 56
9.3 Savepoints 57
9.3.1 Setting and Rolling Back to a Savepoint 57
9.3.2 Releasing a Savepoint 58
10. Connections 59
10.1 Types of Drivers 60
10.2 The Driver Interface 60
10.2.1 Loading a driver that implements java.sql.Driver 61
10.3 The DriverManager Class 61
10.3.1 The SQLPermission Class 63
10.4 The DataSource Interface 63
10.4.1 DataSource Properties 64
10.4.2 The JNDI API and Application Portability 65
10.4.3 Getting a Connection with a DataSource Object 66
11. Connection Pooling 67
11.1 ConnectionPoolDataSource and PooledConnection 69
11.2 Connection Events 70
11.3 Connection Pooling in a Three-tier Environment 71
11.4 DataSource Implementations and Connection Pooling 72
11.5 Deployment 74
11.6 Reuse of Statements by Pooled Connections 75
11.6.1 Using a Pooled Statement 76
11.6.2 Closing a Pooled Statement 77
11.7 ConnectionPoolDataSource Properties 78
12. Distributed Transactions 81
12.1 Infrastructure 81
12.2 XADataSource and XAConnection 84
12.2.1 Deploying an XADataSource Object 85
12.2.2 Getting a Connection 86
12.3 XAResource 86
12.4 Transaction Management 87
12.4.1 Two-phase Commit 88
12.5 Closing the Connection 90
12.6 Limitations of the XAResource Interface 90
13. Statements 93
13.1 The Statement Interface 93
13.1.1 Creating Statements 93
13.1.1.1 Setting ResultSet Characteristics 94
13.1.2 Executing Statement Objects 94
13.1.2.1 Returning a ResultSet object 95
13.1.2.2 Returning an Update Count 95
13.1.2.3 Using the Method execute 95
13.1.3 Closing Statement Objects 96
13.2 The PreparedStatement Interface 97
13.2.1 Creating a PreparedStatement Object 97
13.2.1.1 Setting ResultSet Characteristics 97
13.2.2 Setting Parameters 98
13.2.2.1 Type Conversions 98
13.2.2.2 National Character Set Conversions 99
13.2.2.3 Type Conversions Using the Method setObject 99
13.2.2.4 Setting NULL Parameters 100
13.2.3 Describing Outputs and Inputs of a PreparedStatement
Object 101
13.2.4 Executing a PreparedStatement Object 102
13.2.4.1 Returning a ResultSet Object 102
13.2.4.2 Returning a Row Count 102
13.2.4.3 Using the Method execute 103
13.3 The CallableStatement Interface 103
13.3.1 Creating a CallableStatement Object 104
13.3.2 Setting Parameters 104
13.3.2.1 IN Parameters 105
13.3.2.2 OUT Parameters 105
13.3.2.3 INOUT Parameters 106
13.3.3 Executing a CallableStatement Object 106
13.3.3.1 Returning a Single ResultSet Object 107
13.3.3.2 Returning a Row Count 107
13.3.3.3 Returning Unknown or Multiple Results 107
13.4 Escape Syntax 109
13.4.1 Scalar Functions 110
13.4.2 Date and Time Literals 111
13.4.3 Outer Joins 111
13.4.4 Stored Procedures 112
13.4.5 LIKE Escape Characters 113
13.5 Performance Hints 113
13.6 Retrieving Auto Generated Keys 113
歡迎提出修改意見
Chapter9 - Chapter13
9.事務
---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 53
9.1 Transaction Boundaries and Auto-commit 53
9.1.1 Disabling Auto-commit Mode 54
9.2 Transaction Isolation Levels 55
9.2.1 Using the setTransactionIsolation Method 56
9.2.2 Performance Considerations 56
9.3 Savepoints 57
9.3.1 Setting and Rolling Back to a Savepoint 57
9.3.2 Releasing a Savepoint 58
10. Connections 59
10.1 Types of Drivers 60
10.2 The Driver Interface 60
10.2.1 Loading a driver that implements java.sql.Driver 61
10.3 The DriverManager Class 61
10.3.1 The SQLPermission Class 63
10.4 The DataSource Interface 63
10.4.1 DataSource Properties 64
10.4.2 The JNDI API and Application Portability 65
10.4.3 Getting a Connection with a DataSource Object 66
11. Connection Pooling 67
11.1 ConnectionPoolDataSource and PooledConnection 69
11.2 Connection Events 70
11.3 Connection Pooling in a Three-tier Environment 71
11.4 DataSource Implementations and Connection Pooling 72
11.5 Deployment 74
11.6 Reuse of Statements by Pooled Connections 75
11.6.1 Using a Pooled Statement 76
11.6.2 Closing a Pooled Statement 77
11.7 ConnectionPoolDataSource Properties 78
12. Distributed Transactions 81
12.1 Infrastructure 81
12.2 XADataSource and XAConnection 84
12.2.1 Deploying an XADataSource Object 85
12.2.2 Getting a Connection 86
12.3 XAResource 86
12.4 Transaction Management 87
12.4.1 Two-phase Commit 88
12.5 Closing the Connection 90
12.6 Limitations of the XAResource Interface 90
13. Statements 93
13.1 The Statement Interface 93
13.1.1 Creating Statements 93
13.1.1.1 Setting ResultSet Characteristics 94
13.1.2 Executing Statement Objects 94
13.1.2.1 Returning a ResultSet object 95
13.1.2.2 Returning an Update Count 95
13.1.2.3 Using the Method execute 95
13.1.3 Closing Statement Objects 96
13.2 The PreparedStatement Interface 97
13.2.1 Creating a PreparedStatement Object 97
13.2.1.1 Setting ResultSet Characteristics 97
13.2.2 Setting Parameters 98
13.2.2.1 Type Conversions 98
13.2.2.2 National Character Set Conversions 99
13.2.2.3 Type Conversions Using the Method setObject 99
13.2.2.4 Setting NULL Parameters 100
13.2.3 Describing Outputs and Inputs of a PreparedStatement
Object 101
13.2.4 Executing a PreparedStatement Object 102
13.2.4.1 Returning a ResultSet Object 102
13.2.4.2 Returning a Row Count 102
13.2.4.3 Using the Method execute 103
13.3 The CallableStatement Interface 103
13.3.1 Creating a CallableStatement Object 104
13.3.2 Setting Parameters 104
13.3.2.1 IN Parameters 105
13.3.2.2 OUT Parameters 105
13.3.2.3 INOUT Parameters 106
13.3.3 Executing a CallableStatement Object 106
13.3.3.1 Returning a Single ResultSet Object 107
13.3.3.2 Returning a Row Count 107
13.3.3.3 Returning Unknown or Multiple Results 107
13.4 Escape Syntax 109
13.4.1 Scalar Functions 110
13.4.2 Date and Time Literals 111
13.4.3 Outer Joins 111
13.4.4 Stored Procedures 112
13.4.5 LIKE Escape Characters 113
13.5 Performance Hints 113
13.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中的JDBC
5.類和接口
---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 不贊成使用的API
7.數據庫元數據(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 1
Typographic Conventions 2
Submitting Feedback 2
1. Introduction 3
1.1 The JDBC API 3
1.2 Platforms 3
1.3 Target Audience 4
1.4 Acknowledgements 4
2. Goals 7
2.1 History 7
2.2 Overview of Goals 7
3. Summary of New Features 11
3.1 Overview of changes 11
4. Overview 15
4.1 Establishing a Connection 15
4.2 Executing SQL Statements and Manipulating Results 16
4.2.1 Support for SQL Advanced Data Types 17
4.3 Two-tier Model 17
4.4 Three-tier Model 18
4.5 JDBC in the J2EE Platform 20
5. Classes and Interfaces 21
5.1 The java.sql Package 21
5.2 The javax.sql Package 26
6. Compliance 31
6.1 Definitions 31
6.2 Guidelines and Requirements 32
6.3 JDBC 1.0 API Compliance 33
6.4 JDBC 2.0 API Compliance 33
6.5 JDBC 3.0 API Compliance 34
6.6 JDBC 4.0 API Compliance 35
6.7 Determining Compliance Level 35
6.8 Deprecated APIs 36
7. Database Metadata 39
7.1 Creating a DatabaseMetadata Object 40
7.2 Retrieving General Information 40
7.3 Determining Feature Support 41
7.4 Data Source Limits 41
7.5 SQL Objects and Their Attributes 42
7.6 Transaction Support 42
7.7 New Methods 42
7.8 Modified Methods 43
8. Exceptions 45
8.1 SQLException 45
8.1.1 Support for the J2SE Chained Execeptions 46
8.1.2 Navigating SQLExceptions 46
8.1.2.1 Using a For-Each Loop with SQLExceptions 47
8.2 SQLWarning 48
8.3 DataTruncation 48
8.3.1 Silent Truncation 49
8.4 BatchUpdateException 49
8.5 Categorized SQLExceptions 50
8.5.1 NonTransient SQLExceptions 50
8.5.2 Transient SQLExceptions 51
歡迎提出修改意見!
JSR 221
Early Draft
June 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.