jdbcTemplate使用總結1

luxika發表於2011-09-20
SqlRowSet rs = jdbcTemplate.queryForRowSet(sql, params);
Tips1: jdbcTemplate有很多的ORM化回撥操作將返回結果轉為物件列表,但很多時候還是需要返回ResultSet,Spring有提供一個類似ResultSet的,實現JDBC3.0 RowSet介面的 Spring SqlRowSet。兩個類似的類,RowSet簡單的封裝了一層resultset,使用起來可能有點區別,使用jdbcTemplate.queryForRowSet()遇到UncategorizedSQLException:Invalid precision value. Cannot be less than zero的異常,原因:
it was caused by Oracle's drivers not correctly supporting the CachedRowSet interface.
解決辦法是creating an ResultSetExctractor class that implements ResultSetExtractor and used that to get an OracleCachedRowSet implementation of SqlRowSet. To use this you will need to include the oracle driver jar in your project (used ojdbc5.jar).

Tips2:.注意jdbcTemplate儘量只執行查詢操作,莫要進行更新,否則會破壞Hibernate的二級快取體系。