今天看了java7的预览版,过一段就会出测试版,测试版应该不会再加功能,只是修改bug。今天研究的是java7的新功能jdbc4。JDBC4一个 重要的新特性是在连接数据库后不用手动的关闭java.sql.Connection, java.sql.Statement, and java.sql.ResultSet这些对象。只要这些对象在try模块中声明,JVM会自动关闭这些对象。下面就是一个简单的例子:
public static void viewTable(Connection con) throws SQLException {
String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + ", " + supplierID + ", " + price +
", " + sales + ", " + total);
}
} catch (SQLException e) {
JDBCTutorialUtilities.printSQLException(e);
}
}
下面还有一些小的新特性,等我整理了再发出来!如果不好,请大家提意见,请大家高抬贵手不要打隐藏贴,谢谢!