JDBC 4.1, which is part of Java SE 7, introduces the following features:
try
-with-resources statement to automatically close resources of type Connection
, ResultSet
, and Statement
RowSetFactory
interface and the RowSetProvider
class, which enable you to create all types of row sets supported by your JDBC driver.You can use a try
-with-resources statement to automatically close java.sql.Connection
, java.sql.Statement
, and java.sql.ResultSet
objects, regardless of whether a SQLException
or any other exception has been thrown. A try
-with-resources statement consists of a try
statement and one or more declared resources (which are separated by semicolons).
See The try-with-resources Statement for more information
The following example uses a try
-with-resources statement to close the Statement
object, stmt
, automatically:
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); } } }
The following statement is a try
-with-resources statement that declares one resource, stmt
, which will be automatically closed then the try
block terminates:
try (Statement stmt = con.createStatement()) { // ... }
You can use an instance of RowSetFactory
to create a RowSet
object. The following example uses an instance of RowSetFactory
to create the JdbcRowSet
object, jdbcRs
:
public void testJdbcRowSet(String username, String password) throws SQLException { RowSetFactory myRowSetFactory = null; JdbcRowSet jdbcRs = null; ResultSet rs = null; Statement stmt = null; try { myRowSetFactory = RowSetProvider.newFactory(); jdbcRs = myRowSetFactory.createJdbcRowSet(); jdbcRs.setUrl("jdbc:myDriver:myAttribute"); jdbcRs.setUsername(username); jdbcRs.setPassword(password); jdbcRs.setCommand("select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES"); jdbcRs.execute(); // ...
The following statement creates the RowSetProvider
object myRowSetFactory
with the default RowSetFactory
implementation, com.sun.rowset.RowSetFactoryImpl
:
myRowSetFactory = RowSetProvider.newFactory();
Alternatively, if your JDBC driver has its own RowSetFactory
implementation, you can specify it as an argument of newFactory
.
The following statements create the JdbcRowSet
object jdbcRs
and configure its database connection properties:
jdbcRs = myRowSetFactory.createJdbcRowSet(); jdbcRs.setUrl("jdbc:myDriver:myAttribute"); jdbcRs.setUsername(username); jdbcRs.setPassword(password);
The RowSetFactory
interface contains methods to create the different types of RowSet
implementations available in JDBC 4.1 and later:
createCachedRowSet
createFilteredRowSet
createJdbcRowSet
createJoinRowSet
createWebRowSet