我想使用可滚动的结果集,所以当我使用两行代码时:
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;
}
});
}
}
这是异常(exception)
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;
}
});
最佳答案
首先,ResultSet.TYPE_SCROLL_SENSITIVE
是一个指示结果集类型 的常量,当然不是 setFetchDirection
的有效参数,它需要一个 < em>正确的方向。引用 ResultSet#setFetchDirection(int direction)
的 javadoc 的参数部分:
direction
- anint
specifying the suggested fetch direction; one ofResultSet.FETCH_FORWARD
,ResultSet.FETCH_REVERSE
, orResultSet.FETCH_UNKNOWN
因此出现异常和消息“调用中的参数无效:setFetchDirection
”。
顺便说一句,根据 Oracle 的“JDBC Developer's Guide and Reference”(所有版本都可以从 http://tahiti.oracle.com/ 获得)在 Processing a Scrollable Result Set 中:
Presetting the Fetch Direction
The JDBC 2.0 standard allows the ability to pre-specify the direction, known as the fetch direction, for use in processing a result set. This allows the JDBC driver to optimize its processing. The following result set methods are specified:
void setFetchDirection(int direction) throws SQLException
*int getFetchDirection() throws SQLException
The Oracle JDBC drivers support only the forward preset value, which you can specify by inputting the
ResultSet.FETCH_FORWARD
static constant value.The values
ResultSet.FETCH_REVERSE
andResultSet.FETCH_UNKNOWN
are not supported. Attempting to specify them causes a SQL warning, and the settings are ignored.
readme 中也提到了这一点的 Oracle Database 11g Release 2 JDBC Drivers (撰写本文时的最终版本):
The scrollable result set implementation has the following limitation:
- setFetchDirection() on ScrollableResultSet does not do anything.
但所有这些都是一种旁注,使用 setFetchDiretion
根本不是获得可滚动结果集的方法。
要使用 Spring 的 JdbcTemplate
创建可滚动的结果集,您实际上应该使用方法 execute(CallableStatementCreator csc, CallableStatementCallback action)
与自定义 CallableStatementCreator
执行。在此实现中,使用方法 Connection.prepareCall(String sql, int resultSetType, int resultSetConcurrency)
创建一个 CallableStatement
,它将生成具有给定类型和并发性的 ResultSet
对象。最后,调用 rs.absolute()
。
更新: connection.prepareCall()
调用有问题,第三个参数应该是并发类型(ResultSet.CONCUR_READ_ONLY
或 ResultSet.CONCUR_UPDATABLE
)。试试这个:
return connection.prepareCall(
"{call PKG_USER.getUser(?,?)}",
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
关于java - 不能将 resultSet.setFetchDirection(ResultSet.TYPE_SCROLL_SENSITIVE) 与 spring jdbc DaoSupport 与 Oracle 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2068951/