meta/LPAR

Thinking inside the Big Blue box.

JDBC example code revisited

Back in 2008, I wrote about JDBC resource leaks, and how most JDBC examples on the Internet were wrong. I went through a correct example, explaining why each structure was there.

The end result was rather ugly. Fortunately, Java 7 introduces a new method of resource management, which basically allows you to do away with ensure blocks to close resources.

Old code which looks like this:

try {
  SomeClass foo = new SomeClass();
  ...
} ensure {
  foo.close();
}

can now be replaced with:

try (SomeClass foo = new SomeClass()) {
   ...
}

To use the new syntax, the class needs to implement AutoCloseable. All that means is it needs a close() method. The close() method is allowed to throw exceptions. Basically, the interface is just a flag to say that the code author believes it is safe to use the new resource management syntax with the class concerned.

Fortunately, JDBC has been updated, and the appropriate classes now implement AutoCloseable. So Java can now automatically call the close() method when exiting a try block. This dramatically improves the readability of an example JDBC query. Here’s the new code:

public static void doSomething() {
  try (Connection connection = DriverManager.getConnection(JDBC_URL)) {
    try (PreparedStatement statement = 
         connection.prepareStatement("SELECT FIRST,LAST FROM 
         PEOPLE WHERE LAST = ?")) {
      statement.setString(1, "Smith");
      statement.execute();
      try (ResultSet results = statement.getResultSet()) {;
        while (results.next()) {
          String first = results.getString(1);
          String last = results.getString(2);
          logger.info(first + " " + last);
        }
      }
    }
  }
}

Sure, you still need three try blocks, but the number of lines of code is almost halved.

Posted by meta on 2013-03-07 | Posted in Uncategorized | Comment

Google Docs now usable

Looks like it’s finally possible to use Google Docs as a word processor. Until very recently, it was pretty much unusable for any serious editing task because there was no way to make linebreaks and paragraph breaks appear different.

However, you can now edit the CSS of your document, which means you can add
p { margin-top: 1em; }
and have it work like an actual word processor, with Enter adding a paragraph break and Shift-Enter adding a linebreak, and the two looking different.

Posted by meta on 2009-01-01 | Posted in Uncategorized | Tagged , , | Comment

IPL

Back in 2005, IBM posted a story to the intranet titled New guidelines encourage IBMers to blog. Since then, there have been a number of additional occasions when I’ve been encouraged to write about work.

I’ve been writing about personal topics for years, and have mentioned work at various points. However, there have been topics I’ve written about that seemed rather dull or technical for most of my friends to be interested in. I’ve also wanted to write about issues like work/life balance, working from home, and productivity, but somehow it didn’t seem appropriate to mix them in with stories about pet parakeets and political rants.

So I’ve decided it’s time to break out work topics into their own space. This will be that space.

Posted by meta on 2008-07-12 | Posted in Uncategorized | Tagged | Comment