DEV Community

Anatoly
Anatoly

Posted on • Edited on

JDBC and Streams have never been simpler

Just updated a library which makes life much easier when speaking about databases...

When streams were introduced in java I got myself thinking of how I can leverage it with databases. It is "pretty common" task to write SQL queries and process the results within Java. And the more I was ought to deal with prepared statements, result sets, all those SQLExceptions etc., the more I was disappointed. Searching the WEB for solutions where I wanted to find two worlds collided conveniently - I found no suitable one! Of course there are many libraries that afford some, but neither of them suited me. I wanted something else. Therefore I decided to write one myself for myself.

My goals...

...were based on my background experience:

  1. No prepared boilerplate and SQLExceptions
  2. Executing arbitrary scripts
  3. Minimal configuration
  4. "Real" streams (lazily data fetching)
  5. Raw SQL
  6. Database is just a method invocation

and some others - don't remember them all :)

So after some time I ended up with this:

A brief

1 - Get:

<dependency>
  <groupId>com.github.buckelieg</groupId>
  <artifactId>jdbc-fn</artifactId>
  <version>1.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

2 - Setup:

DB db = DB.builder()
        .withMaxConnections(10)
        .build(() -> DriverManager.getConnection("vendor-specific-string"));
Enter fullscreen mode Exit fullscreen mode

3 - Use:

Collection<String> names = db.select("SELECT name FROM users WHERE ID IN (?, ?)", 1, 2).execute(rs -> rs.getString("name")).collect(Collectors.toList());
Enter fullscreen mode Exit fullscreen mode

Happy to share: jdbc-fn project.

Top comments (0)