繁体   English   中英

如何从ResultSet java sql包中获取所有剩余的行

[英]How to get all remaining rows from ResultSet java sql package

我曾经使用com.datastax.driver.core.ResultSet。 我可以将所有剩余的行放入模型类的数组中,然后将其添加到数组列表中。

下面是我以前使用datastax包中的ResultSet编写的代码。

List<ReportDescription[]> result = new ArrayList<>();
ReportDescription[] csvReportDescriptions;
csvReportDescriptions = resultSet.all().stream()
                    .map(row -> new ReportDescription(row.getObject("value"))).toArray(ReportDescription[]::new);
result.add(csvReportDescriptions);

现在,我更改数据库,所以现在需要将ResultSet切换到java.sql.ResultSet包。 是否有可能像以前一样获取所有行,创建模型的新实例并将其放入数组列表中?

我是这样靠自己做的

try {
ResultSet rS = dataSource.getConnection().createStatement().executeQuery(query.toString());
    while(rS.next()){
        descriptions.add(new ReportDescription(rS.getObject("VALUE")));
    }
    } catch (SQLException e) {
                dataSource.logError("Can't execute statement :" + query.toString());
    }               

    csvReportDescriptions = descriptions.toArray(new ReportDescription[descriptions.size()]);
    result.add(csvReportDescriptions);

您正在尝试使用Streams以Java8的方式从ResultSet中检索数据,因此有多种方法可以做到这一点。

我们如何使用JDBCJava 7中编写SQL

List<Schema> result = new ArrayList<>();
try (Connection c = getConnection()) {
    String sql = "select schema_name, is_default " +
                 "from information_schema.schemata " +
                 "order by schema_name";

    try (PreparedStatement stmt = c.prepareStatement(sql);
         ResultSet rs = stmt.executeQuery()) {

        while (rs.next()) {
            System.out.println(
                new Schema(rs.getString("SCHEMA_NAME"),
                           rs.getBoolean("IS_DEFAULT"))
            );
        }
    }
}

我们如何使用jOOλJava 8中编写SQL

try (Connection c = getConnection()) {
    String sql = "select schema_name, is_default " +
                 "from information_schema.schemata " +
                 "order by schema_name";

    try (PreparedStatement stmt = c.prepareStatement(sql) {

        // We can wrap a Statement or a ResultSet in a
        // Java 8 ResultSet Stream
        SQL.stream(stmt, Unchecked.function(rs ->
            new Schema(
                rs.getString("SCHEMA_NAME"),
                rs.getBoolean("IS_DEFAULT")
            )
        ))
        .forEach(System.out::println);
    }
}

我们如何使用jOOQJava 8中编写SQL

try (Connection c = getConnection()) {
    String sql = "select schema_name, is_default " +
                 "from information_schema.schemata " +
                 "order by schema_name";

    DSL.using(c)
       .fetch(sql)

       // We can use lambda expressions to map jOOQ Records
       .map(rs -> new Schema(
           rs.getValue("SCHEMA_NAME", String.class),
           rs.getValue("IS_DEFAULT", boolean.class)
       ))

       // ... and then profit from the new Collection methods
       .forEach(System.out::println);
}

我们如何使用Spring JDBCJava 8中编写SQL

try (Connection c = getConnection()) {
    String sql = "select schema_name, is_default " +
                 "from information_schema.schemata " +
                 "order by schema_name";

    new JdbcTemplate(
            new SingleConnectionDataSource(c, true))

        // We can use lambda expressions as RowMappers
        .query(sql, (rs, rowNum) ->
            new Schema(
                rs.getString("SCHEMA_NAME"),
                rs.getBoolean("IS_DEFAULT")
            ))

        // ... and then profit from the new Collection methods
        .forEach(System.out::println);
}

资料来源: JOOQ

简短答案:您不能。 这是因为ResultSet最初是通过对具有游标支持的数据库的支持而创建的-您的所有结果甚至可能都不在数据库服务器的内存中,也无法实际下载所有结果。

您可以做的就是简单地遍历结果集,并借助一些样板代码将所有结果添加到列表中:

List<ReportDescription> descs = new ArrayList<>();
while (resultSet.next()) {
    descs.add(new ReportDescription(resultSet.getObject("value")));
}

编辑:要扩展答案ResultSet实际上总是将光标保持在一行上(或特殊的准行: 在firstlast 之前 )。 因此,您在ResultSet上调用的任何getter实际上都会从光标指向的行中获取相应的数据(如果不在任何行上,则抛出异常)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM