Вы пытаетесь извлечь данные из ResultSet
способом Java8, используя Streams
, поэтому есть несколько способов сделать это.
Как мы пишем SQL в Java 7 , используя 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"))
);
}
}
}
Как мы пишем SQL в Java 8 , используя 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);
}
}
Как мы пишем SQL в Java 8 , используя 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);
}
Как мы пишем SQL в Java 8 , используя 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);
}
Источник: JOOQ