Java 6 comes with a number of neat new JDBC features, including an integrated, in-memory database. In fact, one of the more talked-about additions to the Java 6 JDK is Java DB
, an integrated distribution of the Apache Derby (formerly IBM Cloudscape) database. Apache Derby is a lightweight, fully transactional, 100 percent Java embedded database. It is widely used in many open source Java applications, as it lets you simplify the installation process by providing a viable default in-memory database out of the box.
The built-in database lets developers easily set up and run fast-running unit tests against a real database, without the hassle of setting up and managing a test database environment. And by the time it gets into the production release, it should also support JDBC 4.0.
In Java 6, you no longer need to explicitly load the JDBC class. Remember the old Class.forName("oracle.jdbc.driver.OracleDriver")? In JDBC, you don't need this line; the driver will be loaded whenever you obtain a new Connection from the DriverManager class, as shown here:
This will work with JDBC-compliant drivers, which are required to provide a META-INF/services/java.sql.Driver file. This file contains the class or classes that implement java.sql.Driver.
Now consider an example of what you will be able do with Derby and JDBC 4. First of all, you would create a new database. Suppose it is a simple in-memory database with just one CLIENT table and some sample data. You could create it as follows:
Statement stmt = conn.createStatement();
stmt.execute("create table CLIENT(ID int primary key,
stmt.execute("insert into CLIENT values (1, 'Joe', 'Bloggs')");
stmt.execute("insert into CLIENT values (2, 'Jane', 'Doe')");
stmt.execute("insert into CLIENT values (3, 'John', 'Smith')");
Pretty easy stuff. This is typically the sort of thing you might do to set up an in-memory database for unit testing purposes. Note that other than the lack of the Class.forName() statement this code would also work with Derby in Java 5. From here on, however, the example leaves the realms of Java 5 completely and ventures into the new and uncharted lands of JDBC 4 and Java 6.