Domanda

I'm working on a project which has the constraint of using java 1.5. My problem is that it is full of boilerplate like this:

Query q = null;
   try {
    q = getTemplatedQuery("updateConfigurationSyncTimestamps").prepareQuery(false);
    q.addParameter("id", copyConfiguration.getId())
        .addParameter("targetLastSyncTime", targetSyncTime)
        .addParameter("targetLastSuccessfullSyncTime", lastSyncErrors == 0 ? targetSyncTime : null)
        .addParameter("lastSyncErrors", lastSyncErrors);
    q.executeUpdate();
   } finally {
    if (q != null) {
     q.closeStatement();
    }
   }

How can I solve this problem in java 1.5? What I'm trying to achieve is to be able to use a construct like the try-with-resources in java 1.7 or something similar. I just want to close a statement no matter what happens while the program is running but without the boilerplate in the example.

È stato utile?

Soluzione

You can use the Apache Commons IOUtils.closeQuietly() in your finally block to reduce it down to one line.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top