Skip to content

Latest commit

 

History

History
71 lines (62 loc) · 3.34 KB

README.md

File metadata and controls

71 lines (62 loc) · 3.34 KB

#A fluent JDBC wrapper with lambdas.

Those who are not convinced to use Hibernate to manage persistence are forced to use plain old JDBC API. Though powerfull it requires lot of typing to get it right. For example retrieving user data from database often requires such code snippet:

final List<User> result = new ArrayList<>();
final PreparedStatement s = 
        conn.prepareStatement("select name, password from user where name = ?");
try{
   s.setString(1, "adam");
   final ResultSet rs = s.executeQuery();
   try {
      result.add(new User(rs.getString(1), rs.getString(2)));
   } finally {
      rs.close();
   }
} finally {
   s.close();
}

With Java 7’s try-with-resources this code is a little shorter:

List<User> list = new ArrayList<>();
    try (final PreparedStatement s = c.prepareStatement("select name, password from users where name = ?")) {
        s.setString(1, "adam");
        try (final ResultSet rs = s.executeQuery()) {
            list.add(new User(rs.getString(1), rs.getString(2)));
        }
    }

With Java 8’s lambda expressions and FluentConnection wrapper this code can be delimited even further to something like this.

List<User> list2 = using(c).prepare("select name, password from users where name = ?").
              set("Adam").andMap(rs -> new User(rs.getString(1), rs.getString(2)));

The wrapper uses static imports (in form of import static fluentJDBC.FluentConnection.*;), fluent method chaining, with implicit parameter index incrementation and functional interfaces which allow lambda expression as parameters to andMap, andReduse and forEach methods. Other examples of FluentConnection uage:

Connection c = null; // we surely should have initializeded this :)

//FluentJDBC - single value retrieval
Optional<User> user = using(c).prepare("select name, password from users where name = ?").
	set("Adam").andMapOne(rs -> new User(rs.getString(1), rs.getString(2)));

//FluentJDBC - result printing
using(c).prepare("select name, password from users where name = ?").
	set("Adam").andForEach(rs -> System.out.println(rs.getString(1)));

// FluentJDBC - aggregation in application - inefficient example
String str = using(c).prepare("select name, password from users where name = ?").
	set("Adam").andReduce("", (acc, rs) -> acc + "name: " + rs.getString(1));

//FluentJDBC - insertion (works for modification and deletion as well)
using(c).prepare("insert into users values (?,?,?)").set(1).set("Adam").
	set("password").andUpdate();

//FluentJDBC - insertion returning autogenerated primary key
Integer id = (Integer) using(c).
	prepare("insert into users(name, password) values (?,?)", true).
	set("Adam").set("secret").andUpdateReturningKey();

//FluentJDBC - custom parameter setting code
using(c).prepare("insert into users values (?,?,?)").set(1).set("Adam").
	set((s, index) -> s.setString(index, "secret")).andUpdate();

//FluentJDBC - custom PreparedStatement modification
using(c).prepare("insert into users values (?,?,?)").set(1).
	set("Adam").set("password").
	apply((s) -> s.setCursorName("c1")).andUpdate();

FluentJDBC is a BSD licensed, single file (FluentConnection.java), lightweight wrapper that You can include in Your project and use. It is by no means a complete JDBC wrapper, but I encourage You to extend it to meet Your own needs.