|
From: <tho...@tr...> - 2004-06-17 15:29:46
|
We talked about providing something more robust than queryForList. I have
looked at the new RowSet implementation and it looks pretty good. The only
problem is that it thows SqlExceptions.
I have created two new interfaces, one SqlRowSet that wraps a RowSet and
rethrows any SqlException as a DataRetrievalFailureException. The other one is
SqlRowSetMetaData that wraps the RowSetMetaData and it throws
DataAccessResourceFailureException instead of SqlException. I only included
the methods that made sense to me in these interfaces. I also provided
implementations for these interfaces.
Right now these classes are experimental - to be included in 1.1 - need javadocs
and tests and maybe some refinements. At the moment we just create a
CachedRowSet implementation that is provided in the sun RowSet 1.0 download. We
could make this pluggable in the future and support any implementation of a
CachedRowSet. The Sun implementation will be part of Java 1.5, so going
forward we would not even need an extra rowset.jar file.
The drawback with the RowSet implementation is that with Java 1.4, we need an
additional jar file and we also need a JDBC driver that is up to snuff.
Oracle's 10g driver works, but not the 9i drivers. The PostgreSQL driver does
not work, but the Microsoft SQL Server driver is fine. Anybody using
DB2/Sybase? I'll try to test MySQL this weekend.
The queryForRowSet methods are in a JdbcTemplateExt extension class in the
sandbox right now - we should merge then into the current JdbcTemplate when we
move the code out of the sandbox.
This is how to use it:
(RowSet extends ResultSet so everybody should know how to get at the data :-)
JdbcTemplateExt jtx = new JdbcTemplateExt(ds);
SqlRowSet sqlRowSet = jtx.queryForRowSet(sql);
System.out.println("**" + sqlRowSet.getCommand());
while (sqlRowSet.next()) {
System.out.print(" " + sqlRowSet.getString(1));
System.out.print(" | " + sqlRowSet.getInt(2));
System.out.println(" | " + sqlRowSet.getDate("order_date"));
}
Performance is on par with queryForList, if not slightly faster.
Thomas
|