Java/JVM Programmers -- How do you interact with a DB?

petard

Ars Scholae Palatinae
1,379
Subscriptor++
What do you mean by "Bespoke JDBC"?

I just use JDBC with an appropriate driver... I'm only occasionally made to use java, but when I do the drill always looks something like (WARNING: code composed in forum not an IDE)

Code:
Connection connection =  DriverManager.getConnection(connectionString);
Statement statement = connection.createStatement();
// execute some sql with statement and process results

I always just thought this was how anyone who didn't want to deal with ORM did things :). But I would have just called this out-of-box JDBC. There's really nothing bespoke about it. I'm just using JDBC as intended. Thus my question about what those first choices mean.
 
Do you create all the JDBC-based methods as you need them or did you create your own "generic" JDBC framework that just executes queries on your behalf?

for instance:
Code:
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;
      }
    }
  }
}

OR is it more like
Code:
public List<Widget> fetchWidgets() throws SQLException {
  List<Widgets> ret = (List<Widget>) customFetcher(FETCH_WIDGETS_SQL);
  return ret;
}

Edit: Note that "customFetcher" could work somewhat like DbUtils, but would be something you built in-house.
 

Drizzt321

Ars Legatus Legionis
34,938
Subscriptor++
Spring JDBC, and some parts of our main application Hibernate (ugh), and in a newer application MyBatis. Which is sorta a ORM-ish. Lets you write SQL fragments, as well as using conditionals/loops/etc, and a Mapping which you setup to map tables/fields to your POJO/Bean entities.

Also as an aside, using JaVers for entity versioning, and have it working well through a fairly straightforward setup, although a few bits are Aspects for certain bits which make it more complicated. Security/permissions are some of those, which include Roles which can have a limited set of fields on different entities available to see.
 
D

Deleted member 43669

Guest
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 return 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.

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.
 

petard

Ars Scholae Palatinae
1,379
Subscriptor++
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 return 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.

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.

Interesting. Thanks. I have long considered Spring very heavy and EJB-centric, and I didn't really do EJB stuff, so I didn't pay much attention to it. (That probably dates the last time I looked at it pretty accurately.) Looking at the site now, it seems to have changed a lot. I'll have to evaluate more carefully next time I start a project.

You're right about the boilerplate. My copy/pasted boilerplate has probably lived through too many projects now.
 

gregatron5

Ars Legatus Legionis
13,007
Subscriptor++
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.
 

Drizzt321

Ars Legatus Legionis
34,938
Subscriptor++
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.

MyBatis I don't think has a DSL, you write SQL snippets that you can piece together based on XML configuration/macros/code (supports basic logic/loops/etc). And then you have a mapping for your model objects to DB fields, how to do that, how to do the relations, and then you have an Interface which it does run-time code generation/compilation (I believe) to generate the actual code under the hood. Or maybe it uses aspects on those interfaces to redirect to the actual bits of code that when run generate the proper code-paths/output.
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
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.
There are still lots of cases where you lose type info, or need to duplicate it in the method call.

For example, you often have to call this overload when the type parameters in your generated class's Field<T> are lost due to type erasure. Java!