Building DB-Independent SQL strings in Java

The eden-sql library

This page presents Eden Foundation's library for building SQL strings. It is meant to help the programmer build sql statements in a database-independent fashion, making it simple to move the same code to another database.

Click here to download the library (last updated 2008-01-21)

You are free to use this library, which includes the source code, under the conditions of the LGPL license.
This library is not complete, but most basic functions are already implemented and working. Eden expands it as the internal need arises, but any contribution from outside is welcome, hence the choice of license!

How to use the library

The first thing you need to do is obtain an instance of the SQLFormatter class. Preferably, this should be done using the static method SQLFormatter.get(), as this method tries to analyse your current database connection so that it can return an appropriate subclass. After this, you use the methods of SQLFormatter to obtain sql statements or substatements. Here is a short example:

Statement myStatement = [...];  // we assume you obtained this somewhere...
Integer valueA = 15;
String valueB = "test";
Date valueC = new Date();
SQLFormatter formatter = SQLFormatter.get(myStatement);
String sqlInsertStatement = formatter.createInsertIntoStatement(
				"MyTable", 
				new String[] {"FieldA", "FieldB", "FieldC"}, 
				new Object[] {valueA, valueB, valueC});

The library will take care of checking the data type of the data values supplied, and format the sql statement accordingly.

Read Eden's latest article:
Empowering Teenage Girls in Africa

Contact

For feedback, please contact Josef Garvi on "josef at eden-foundation dot org".


Eden Foundation Home Page