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