直到今天,我一直ResultSet
在处理查询结果。但今天我读了一点RowSet
,CachedRowset
我意识到它们可以更好地服务于我的目的。虽然在所有示例中我都阅读了 whereRowSet
并被CachedRowSet
称为对象,但当我自己在代码中尝试时,我意识到这些是接口,并且在示例中它们使用了这些接口的一些实现。
现在我的问题是我在哪里可以找到这些实现,有什么官方的吗?
我需要下载它们还是它们随 JDK 一起提供?
直到今天,我一直ResultSet
在处理查询结果。但今天我读了一点RowSet
,CachedRowset
我意识到它们可以更好地服务于我的目的。虽然在所有示例中我都阅读了 whereRowSet
并被CachedRowSet
称为对象,但当我自己在代码中尝试时,我意识到这些是接口,并且在示例中它们使用了这些接口的一些实现。
现在我的问题是我在哪里可以找到这些实现,有什么官方的吗?
我需要下载它们还是它们随 JDK 一起提供?
The implementations are JRE specific. Oracle (Sun) JRE comes with a bunch of implementations:
com.sun.rowset.JdbcRowSetImpl
com.sun.rowset.CachedRowSetImpl
com.sun.rowset.WebRowSetImpl
com.sun.rowset.FilteredRowSetImpl
com.sun.rowset.JoinRowSetImpl
In Java 1.6 and before, you'd need to construct them yourself:
JdbcRowSet rowSet = new JdbcRowSetImpl();
rowSet.setDataSourceName("jdbc/dbname");
// Or
rowSet.setUrl("jdbc:vendor://host:port/dbname");
rowSet.setUsername("username");
rowSet.setPassword("password");
rowSet.setCommand("SELECT id, name, value FROM tbl");
rowSet.execute();
while (rowSet.next()) {
// ...
}
In Java 1.7 you can get them by a javax.sql.rowset
factory so that you're not dependent of underlying JRE implementation and that you can finetune the implementation of choice if necessary:
RowSetFactory rowSetFactory = RowSetProvider.newFactory();
JdbcRowSet rowSet = rowSetFactory.createJdbcRowSet();
// ...
It only doesn't provide a possibility to pass a ResultSet
on construction. Those implementations doesn't ship with the average JDBC driver (at least, MySQL and PostgreSQL have none). It's basically an extra (optional) layer over JDBC API as the package name prefix javax
hints.
Note that if you get that far by looking into rowsets, then you might want to consider to look into an ORM instead, such as Hibernate or JPA. They provide first/second level cache possibilities.
javax.sql.rowset
package summary在 Eclipse Java 构建路径中添加 rt.jar。然后你会看到所有的实现类。否则,您可以从 Eclipse 中删除不允许从 jdk 访问 rt.jar 的限制。为我工作。我使用的是 jdk1.6 和 Eclipse Luna。