Chalk Mine - shorthand for embedded SQL in Java

This is the first part of a series I call: Look ma, I invented the wheel again. In a long developer life I have implemented things that have been done by many others, better, before. This is one of them.

Technically Chalk Mine is an ORM (Object Relational Mapper) for Java 5 and later. It provides a simple and concise way of mapping back and forth between Java types and relational data in an SQL-database. In addition to that it provides a way of doing configuration and has built-in connection pool.

This is an example of its typical use:

openConnection();
try {
    int userCount = queryScalar(Int.class, "select count(*) from users where name = ?", "Smith");
    System.out.println("There are " + userCount + " users named 'Smith'.");
} finally {
    closeConnection();
}

The methods openConnection, queryScalar and closeConnection are all static methods in chalkmine.ChalkMine and are here imported using a static import.

The methods openConnection and closeConnection open and close a connection. The connection is bound to the current thread which is why it is not given as an explicit parameter to queryScalar.

The method queryScalar does a query expecting exactly one row otherwise chalkmine.NonScalarException is thrown.

The row is mapped to the type supplied as the first parameter.

Since the type is a simple type (int, double, string etc.) the first column of row is used.

The last parameter ("Smith") as one a variable number of parameters that will be substituted into the ?'s in the query. (This happens using Java's PreparedStatement mechanism taking care of proper escaping of strings etc.)

See more at https://github.com/chvid/chalkmine