簡體   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