March 20, 2007
Using Java DB
Java DB (derived from Apache Derby) is included with the latest Java EE and Java SE platforms. Let's take a look at how to use it.
I've used Java DB quite a bit since it was released with Java EE 5, and I like it. It's well integrated with NetBeans, and writing code to use it is easy. For instance, the figure below shows NetBeans' Runtime View, which can display servers to deploy to, running application processes (related to projects you are developing), a built-in HTTP server, and preconfigured Java DB databases. I purposely blotted out some production databases I'm currently working on.
Figure 1: NetBeans' Runtime View.
When you expand the Databases leaf, you will have access to configured databases. You can right-mouse-click on the lead, and choose "New Connection" from the pop-up menu, to add a new database configuration. The "New Database Connection" dialog box will appear, asking you to enter information about the database you're connecting to, or creating. You can choose the embedded driver (JavaDB runs in the context of your application), or the networked driver (the JavaDB process runs external from your application). The remaining information includes a username, password, and the full URL for your database.
From the figure above, you can see that I'm connected to a sample travel agency database. All of the tables within this database are visible, and you can create new tables through this interface also, along with stored procedures, and specialized database views.
Executing direct queries outside of your application (to test your queries, stored procedures, or just to pre-populate your database) is very easy with NetBeans. Simply right-mouse-click on a database entry, or on a table within a database, and choose "Execute Command" from the pop-up menu. You can write your query in the SQL Editor that opens as a result (see the figure below).
Figure 2: SQL Editor.
Writing JBDC code for JavaDB is similar to other databases. Java EE 5 supports dynamic resource injection, where the application server takes care of linking Java attributes in your code to a physical database. However, there are still cases where I need to write a standalone Java application, or a simple Servlet, where I need to hand craft the JDBC code. The addPerson method below is a JDBC example that connects to a Java DB database and inserts a row into a table:
public void addPerson(Person person)
{
try {
Class.forName(
"org.apache.derby.jdbc.ClientDataSource").newInstance();
Connection conn = DriverManager.getConnection(
"jdbc:derby://localhost:1527/Travel",
"TRAVEL",
"TRAVEL");
String query =
"INSERT INTO PERSON values(?,?,?,?,?)";
PreparedStatement ps = conn.prepareCall(query);
ps.setInt(1, this.genID() );
ps.setString(2, person.name);
ps.setString(3, person.title);
ps.setInt(4, person.freqFlier);
ps.setDate(5, this.genDate() );
ps.execute();
ps.close();
conn.close();
}
catch ( Exception e ) {
e.printStackTrace();
}
}
After being skeptical as to the need for Sun to include a database with both Java EE and Java SE, I'm now sold on Java DB. I've found that it’s very robust, it performs well, and it scales well also. I've used in some demanding production web applications, and it has worked flawlessly, although Sun does mention that it’s better suited to internal corporate applications.
For more information on Java DB, and Apache Derby, click on the following:
- Java DB: http://developers.sun.com/javadb/
- Apache Derby: http://db.apache.org/derby/
Happy coding,
EJB
Posted by Eric Bruno at 10:21 PM Permalink
|