简体   繁体   English

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

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

I used to use com.datastax.driver.core.ResultSet. 我曾经使用com.datastax.driver.core.ResultSet。 I could take all remaining rows and put it into an Array of my model class and then add it to List of Arrays. 我可以将所有剩余的行放入模型类的数组中,然后将其添加到数组列表中。

Below is my previous code with ResultSet from datastax package. 下面是我以前使用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);

Nowadays I change database so now I need to swithch ResultSet to java.sql.ResultSet package. 现在,我更改数据库,所以现在需要将ResultSet切换到java.sql.ResultSet包。 Is it any possibility to get all rows, create new instance of my models and put it into List of Arrays as I did it before? 是否有可能像以前一样获取所有行,创建模型的新实例并将其放入数组列表中?

I make it by my own like that 我是这样靠自己做的

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);

You are trying to retrieve data from ResultSet in a Java8 way using Streams , So there are multiple ways to do it. 您正在尝试使用Streams以Java8的方式从ResultSet中检索数据,因此有多种方法可以做到这一点。

How we write SQL in Java 7 using JDBC 我们如何使用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"))
            );
        }
    }
}

How we write SQL in Java 8 using jOOλ 我们如何使用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);
    }
}

How we write SQL in Java 8 using jOOQ 我们如何使用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);
}

How we write SQL in Java 8 using Spring JDBC 我们如何使用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);
}

Source: JOOQ 资料来源: JOOQ

Short answer: you cannot. 简短答案:您不能。 This is because ResultSet was originally created with support for databases that have cursor support - all of your results might not even be in the database server's memory, nor can it be viable to actually download all of them. 这是因为ResultSet最初是通过对具有游标支持的数据库的支持而创建的-您的所有结果甚至可能都不在数据库服务器的内存中,也无法实际下载所有结果。

What you can do is simply traverse the result set adding all the results to a list with the help of some boilerplate code: 您可以做的就是简单地遍历结果集,并借助一些样板代码将所有结果添加到列表中:

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

EDIT: To expand on the answer - a ResultSet actually always holds a cursor to a row (or a special quasi-row: before first or after last ). 编辑:要扩展答案ResultSet实际上总是将光标保持在一行上(或特殊的准行: 在firstlast 之前 )。 Therefore, any getters you call on a ResultSet actually fetch the respective data from the row the cursor points to (or throw an exception if it's not on any row). 因此,您在ResultSet上调用的任何getter实际上都会从光标指向的行中获取相应的数据(如果不在任何行上,则抛出异常)。

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

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