探秘JDK7新特性之JDBC4.1
JDBC4.1更新了兩個(gè)新特性
1. Connection,ResultSet 和 Statement 都實(shí)現(xiàn)了Closeable 接口,所有在 try-with-resources 語(yǔ)句中調(diào)用,就可以自動(dòng)關(guān)閉相關(guān)資源了
Java代碼
- try (Statement stmt = con.createStatement()){
 - …
 - }
 
2. RowSet 1.1:引入RowSetFactory接口和RowSetProvider類(lèi),可以創(chuàng)建JDBC driver支持的各種 row sets
Java代碼
- RowSetFactory myRowSetFactory = null;
 - JdbcRowSet jdbcRs = null;
 - ResultSet rs = null;
 - Statement stmt = null;
 - try {
 - myRowSetFactory = RowSetProvider.newFactory();//用缺省的RowSetFactory 實(shí)現(xiàn)
 - jdbcRs = myRowSetFactory.createJdbcRowSet();
 - //創(chuàng)建一個(gè) JdbcRowSet 對(duì)象,配置數(shù)據(jù)庫(kù)連接屬性
 - jdbcRs.setUrl("jdbc:myDriver:myAttribute");
 - jdbcRs.setUsername(username);
 - jdbcRs.setPassword(password);
 - jdbcRs.setCommand("select ID from TEST");
 - jdbcRs.execute();
 - }
 
RowSetFactory 接口包括了創(chuàng)建不同類(lèi)型的RowSet的方法
•createCachedRowSet
•createFilteredRowSet
•createJdbcRowSet
•createJoinRowSet
•createWebRowSet
參考資料
Jdk7官網(wǎng) http://openjdk.java.net/projects/jdk7/
(注:這篇文章發(fā)表時(shí),JDK7未正式公布,可能有誤差,具體以官方正式版為準(zhǔn))
【編輯推薦】















 
 
 
 
 
 
 