recent posts related to java, jdbc, spring, etc.

An Effective Java note on reusing existing Exception classes alvin October 29, 2018 - 11:48am

I just read a short chapter in the book Effective Java, and realized I was doing something pretty dumb in my own code by always creating my own custom exceptions instead of using other exceptions already intended to be reused in the Java API.

Java’s javap command doesn’t show private members (by default)

As I was just reminded, Java’s javap command doesn’t show private members by default. You have to use the -p option of javap to see private members.

I was just reminded of that when using the Scala REPL. Given this Person class with a private constructor field named name:

class Person(private var name: String)

javap without the -p option shows this:

Java example: JMenuBar + KeyStroke + AbstractAction

As a brief note, here’s some source code that I used to create a JMenuBar in a Java application. First, I defined some fields in my main class:

private static final KeyStroke fileOpenKeystroke = KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.META_MASK);
private Action fileOpenAction;
private JMenuBar menuBar;

Later in the same class I defined this method:

A printf format reference page (cheat sheet)

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A cool thing about the printf formatting syntax is that the specifiers you can use are very similar, if not identical, between several different languages, including C, C++, Java, Perl, Ruby, and others, so your knowledge is reusable, which is a good thing.

A Java Factory Pattern example

Java Design Patterns FAQ: Can you provide an example of the Factory Pattern in Java?

Sure. In this article we'll look at a small-but-complete example of the Factory Pattern ("Factory Design Pattern") implemented in Java.

How to speed up JDBC PreparedStatement MySQL batch inserts

If you ever need to batch-insert a lot of records into a MySQL/MariaDB database using the JDBC PreparedStatement (as in preparedStatement.executeBatch()) be sure to change the MySQL URL to use the rewriteBatchedStatements, as shown here:


For a recent project I needed to batch-insert about eleven million records into a MySQL database, and the runtime was about 55 minutes. Once I added rewriteBatchedStatements=true to the MySQL URL, the batch-insert time was reduced down to only three minutes. That one little change made all the difference.

A Java method to round a float value to the nearest one-half value

As a quick note, here’s a Java method that will round a float to the nearest half value, such as 1.0, 1.5, 2.0, 2.5, etc.:

 * converts as follows:
 * 1.1  -> 1.0
 * 1.3  -> 1.5
 * 2.1  -> 2.0
 * 2.25 -> 2.5
public static float roundToHalf(float f) {
    return Math.round(f * 2) / 2.0f;

The comments show how this function converts the example float values to their nearest half value, so I won’t add any more details here. I don’t remember the origin of this algorithm — I just found it in some old code, thought it was clever, and thought I’d share it here.