Java 8 中的 SQL:ResultSet Streams。

无论您使用什么 API,使用 Java 8 编写 SQL 将发生根本性的变化。

我们在 Java 7 中如何编写 SQL(使用 JDBC

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

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



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

我们在 Java 8 中如何编写 SQL(使用 jOOQ



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

我们在 Java 8 中如何编写 SQL(使用 Spring JDBC

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

我们在 Java 8 中如何编写 SQL(使用 Apache DbUtils

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

    new QueryRunner()
        .query(c, sql, new ArrayListHandler())

        // We can transform any Collection into a Stream
        .stream()
        .map(array -> new Schema(
            (String) array[0],
            (Boolean) array[1]
        ))

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

有了 Java 8,编写 SQL 终于再次变得有趣了!

访问我们的 Java 8 Friday 博客系列 以了解更多关于使用 Java 8 所带来的重大改进。

上述示例和 Java 8 Friday 博客系列的源代码可以在 GitHub 上找到

The jOOQ Logo