我正在使用H2数据库来读取数据。我正在配置与spring的连接:
<bean id="dataSourceH2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.h2.driver}" />
<property name="url" value="${jdbc.h2.url}" />
<property name="username" value="${jdbc.h2.user}" />
<property name="password" value="${jdbc.h2.password}" />
</bean>
并将其用作:
result = this.namedParameterJdbcTemplateH2.query(
this.queryName, mapParameters, new H2DataExtractor())
非常方便。
直到我意识到从我的查询中返回的结果集是不可滚动的:
org.h2.jdbc.JdbcSQLException:
The result set is not scrollable and can not be reset.
You may need to use conn.createStatementconn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ..).
所以我现在手工创建连接:
Connection conn = null;
try {
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:path/to/my/database/file", "sa", "sa");
final Statement st = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
final ResultSet rs = st.executeQuery("SELECT * FROM SCHEMA1.TABLE1 WHERE id = "
+ id + " AND name = " + name + " ORDER BY color, size" );
resultado = (new CentrosDistribucionDVLExtractor()).extractData(rs);
} catch ....
不太方便。
如何使用spring创建相同的连接,但将结果集设置为ResultSet.TYPE_SCROLL_INSENSITIVE
?