Just curious as to what tools folks use to interact with their DBs.
Connection connection = DriverManager.getConnection(connectionString);
Statement statement = connection.createStatement();
// execute some sql with statement and process results
public List<Widget> fetchWidgets() throws SQLException {
try (Connection conn = dataSource.getConnection()) {
try (PreparedStatement ps = conn.prepareStatement(FETCH_WIDGETS_SQL) {
try (ResultSet rs = ps.executeQuery()) {
List<Widget> ret = new ArrayList<>();
while(rs.next()) {
Widget w = widgetFromRs(rs);
ret.add(w);
}
return ret;
}
}
}
}
public List<Widget> fetchWidgets() throws SQLException {
List<Widgets> ret = (List<Widget>) customFetcher(FETCH_WIDGETS_SQL);
return ret;
}
More like the first I guess, but it's usually just a connection string + a some other pertinent details in a properties file which I read then use JDBC calls directly. I don't generally feel the need to add another layer of indirection above the layer of indirection that is JDBC.
List<Map<String,Object>>, and adds named parameters (e.g. select * from foo where bar = :qux and quux = :xyzzy). Raw JDBC is quite low-level so using a library on top of that cuts out much boilerplate- esp. some *important* boilerplate like closing stuff properly.More like the first I guess, but it's usually just a connection string + a some other pertinent details in a properties file which I read then use JDBC calls directly. I don't generally feel the need to add another layer of indirection above the layer of indirection that is JDBC.
Spring JDBC template (and others, very probably), give you methods that run an SQL query and returnList<Map<String,Object>>, and adds named parameters (e.g.select * from foo where bar = :qux and quux = :xyzzy). Raw JDBC is quite low-level so using a library on top of that cuts out much boilerplate- esp. some *important* boilerplate like closing stuff properly.
You are still doing good ol' SQL and not mapping stuff to objects. And there's probably a good non-Spring equivalent library if you don't like Spring.
JOOq has a generator you can tie into your build system so you get a type-safe DSL to work with. MyBatis does something similar too, I believe.Jooq seems to be as clean and friendly as a Java library can be. There are still lots of cases where you lose type info, or need to duplicate it in the method call, but that's just Java for ya.
JOOq has a generator you can tie into your build system so you get a type-safe DSL to work with. MyBatis does something similar too, I believe.Jooq seems to be as clean and friendly as a Java library can be. There are still lots of cases where you lose type info, or need to duplicate it in the method call, but that's just Java for ya.
There are still lots of cases where you lose type info, or need to duplicate it in the method call.JOOq has a generator you can tie into your build system so you get a type-safe DSL to work with. MyBatis does something similar too, I believe.Jooq seems to be as clean and friendly as a Java library can be. There are still lots of cases where you lose type info, or need to duplicate it in the method call, but that's just Java for ya.