无法将 resultSet.setFetchDirection(ResultSet.TYPE_SCROLL_SENSITIVE) 与 spring jdbc DaoSupport 与 Oracle 一起使用
我想使用可滚动的结果集,所以当我使用两行代码时:
rs.setFetchDirection(ResultSet.TYPE_SCROLL_SENSITIVE);
rs.absolute(12);
在我的 DAOimpl 中,我遇到异常,请帮助解决它们,提前感谢。
import oracle.jdbc.OracleTypes;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Component;
@Component
public class MyDAOimpl extends JdbcDaoSupport implements
MyDAO {
public List<User> getList(final String where) throws Exception {
return (List) getJdbcTemplate().execute(
"{call PKG_USER.getUser(?,?)}",
new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException {
cs.setString(1, where);
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
ResultSet rs = (ResultSet) cs.getObject(6);
rs.setFetchDirection(ResultSet.TYPE_SCROLL_SENSITIVE);
rs.absolute(12);
List<User> list = new ArrayList<User>();
while (rs.next()) {
User user = new User(
rs.getString(1),
rs.getString(2),
rs.getString(3));
list.add(user);
}
return list;
}
});
}
}
这是例外
java.sql.SQLException: Invalid argument(s) in call: setFetchDirection
oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271)
oracle.jdbc.driver.BaseResultSet.setFetchDirection(BaseResultSet.java:128)
//////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
我更改如下,我没有得到任何结果,通常,我的程序返回 100 个用户:
return (List) getJdbcTemplate().execute(new CallableStatementCreator() {
public CallableStatement createCallableStatement(
Connection connection) throws SQLException {
return connection.prepareCall(
"{call PKG_USER.getUser(?,?)}",
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.TYPE_SCROLL_INSENSITIVE);
}
}, new CallableStatementCallback() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException, DataAccessException {
cs.setString(1, where);
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
ResultSet rs = (ResultSet) cs.getObject(6);
//////not run////
rs.absolute(12);
////////////////
List<User> list = new ArrayList<User>();
while (rs.next())
{
List<User> list = new ArrayList<User>();
while (rs.next()) {
User user = new User(
rs.getString(1),
rs.getString(2),
rs.getString(3));
list.add(user);
}
return list;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
ResultSet.TYPE_SCROLL_SENSITIVE
是一个指示结果集类型的常量,并且肯定不是setFetchDirection
的有效参数,它需要一个 fecth方向。引用ResultSet#setFetchDirection(int 方向)
:因此出现异常和消息“调用中的参数无效:
setFetchDirection
”。顺便说一句,根据 Oracle 的“JDBC 开发人员指南和参考< /a>”(所有版本均可从 http://tahiti.oracle.com/ 获得) href="http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/resltset.htm#sthref1169" rel="noreferrer">处理可滚动结果集:
Oracle Database 11g 第 2 版 JDBC 驱动程序(最终版本位于写这篇文章的时候):
但所有这些只是一种旁注,使用 setFetchDiretion 根本不是获取可滚动结果集的方法。
要使用 Spring 的
JdbcTemplate
创建可滚动结果集,您实际上应该使用方法执行(CallableStatementCreator csc, CallableStatementCallback 操作)
与自定义CallableStatementCreator
实现。在此实现中,使用方法Connection.prepareCall(String sql, int resultSetType, int resultSetConcurrency)
创建一个将生成CallableStatement
具有给定类型和并发性的 >ResultSet 对象。最后,调用 rs.absolute()。更新:
connection.prepareCall()
调用存在问题,第三个参数应该是并发类型(ResultSet.CONCUR_READ_ONLY
或ResultSet.CONCUR_UPDATABLE
)。试试这个:First,
ResultSet.TYPE_SCROLL_SENSITIVE
is a constant indicating a result set type and is certainly not a valid argument forsetFetchDirection
which expects a fecth direction. Quoting the parameter section of the javadoc ofResultSet#setFetchDirection(int direction)
:Hence the exception and the message "Invalid argument(s) in call:
setFetchDirection
".And BTW, according to Oracle's "JDBC Developer's Guide and Reference" (all versions are available from http://tahiti.oracle.com/) in Processing a Scrollable Result Set:
This is also mentioned in the readme of the Oracle Database 11g Release 2 JDBC Drivers (the ultimate version at the time of writing this):
But all this was a kind of side note, using
setFetchDiretion
is simply not the way to get a scrollable result set.To create a scrollable result set with Spring's
JdbcTemplate
, you should actually use the methodexecute(CallableStatementCreator csc, CallableStatementCallback action)
with a customCallableStatementCreator
implementation. In this implementation, use the methodConnection.prepareCall(String sql, int resultSetType, int resultSetConcurrency)
to create aCallableStatement
that will produceResultSet
objects with the given type and concurrency. Finally, callrs.absolute()
.UPDATE: There is a problem in the
connection.prepareCall()
call, the third parameter should be a concurrency type (eitherResultSet.CONCUR_READ_ONLY
orResultSet.CONCUR_UPDATABLE
). Try this:从 jdbc 模板获取自定义 ResultSet。 Spring 提供了一个PreparedStatementCreatorFactory,您可以从中创建PreparedStatementCreator。
To get custom ResultSet from jdbc template. Spring has provided a PreparedStatementCreatorFactory from which you can create PreparedStatementCreator.