We wanted to use JDBC with Java Streams, but encountered several difficulties. Fortunately we found solutions with rather small bits of code.
The main obstacle was the JDBC API throwing
all API methods used in our code.
SQLException is a checked
exception, so must be declared in our method signatures, or
caught otherwise. However the Streams API only accepts methods which
declare to throw no checked exceptions, so something simple like
this will not compile:
stream(results) map(row -> row.getString("label")). // checked exception forEach(this::processLabel);
The call to
ResultSet.getString(String) throws a checked
exception. The usual approach is…