Getting Started using Apache Derby in JBuilder - by Lori Olson
By: Lori Olson
Abstract: Using the Apache Derby database involves a lot of setup and configuration. In this article, you will be guided, step-by-step, through the process of installing, configuring, and finally using Derby with one of the JBuilder JDBC sample applications.
Introduction
Installing Derby
Configuring Derby for use in JBuilder
Step 1 – Making Derby accessible to your JBuilder projects.
Step 2 – Making JBuilder aware of Derby
Step 3 – Configuring Derby for use within Database Pilot
Adapting the HelloJDBC sample for use with Derby
Closing Thoughts
I imagine the first question people will ask is --- Why would you want to use Apache Derby in JBuilder, when there is a perfectly good pure JavaTM database, JDataStore, that comes pre-installed and configured for development in every edition of JBuilder, from Enterprise right down to Foundation edition?
As most of you will have discovered by now, JDataStore is a very nice, easy to use, feature complete SQL database, which also happens to be pure JavaTM. But... it is NOT freely distributable. Since IBM has released the database formerly known as Cloudscape to the Apache Foundation, the newly renamed Apache Derby IS freely distributable under the Apache license.
For the purposes of this article, we will be using JBuilder 2006 on Windows Server 2003, and JBuilder 2005 on Mac OS X Tiger. To avoid information overload, we will also limit the usage of Derby to the “embedded” database version. Should this topic prove to be popular, a followup article on the Network Server version can be arranged. We will also assume that you are familiar with JBuilder, projects, libraries, and creating new classes and running them within JBuilder. If you are not familiar with these concepts, I suggest that you go and review the basic tutorials included as a part of JBuilder's Help and Samples before continuing.
Since the point of this article is how to use Derby with JBuilder, I will only give you the outline of installing Derby. A much more detailed discussion of this topic can be found in the Derby Getting Started manual.
DERBY_INSTALL
db-derby-10.1.1.0-bin
java -version
$DERBY_INSTALL/frameworks/embedded/bin
Mac OS X Derby Install Issues:
chmod +x
dos2unix
tr -d '\r' <oldfile >newfile
JBuilder comes with a number of pre-configured JDBC drivers, as well as having built-in support for a number of commercial databases, like Oracle and DB2. But when it comes to adding support for new databases, this multistep process is a frequent cause of frustration. Just remember that you have to complete ALL the steps in order to make your new database fully functional from inside JBuilder.
$DERBY_INSTALL\lib\derby.jar
Non-English users will probably want to add a language-appropriate locale jar (located in the $DERBY_INSTALL\lib directory as well) to the Library paths as well.
$DERBY_INSTALL\lib
Note! Now that you have a library for your Derby driver... you have to actually USE it. Make sure that this library is specified as a “Required Library” in your projects which will be using Derby.
Just because you have created a library to use in your Derby projects, doesn't mean that JBuilder can use it. Now you have to get it into JBuilder's classpath, so that JBuilder and other tools (Database Pilot, etc) can use it.
derby.config
<JBuilder>/lib/ext
$DERBY_INSTALL
addpath
derby.jar
addpath /Users/lori/Applications/db-derby-10.1.1.0-bin/lib/derby.jar
addpath C:/apps/db-derby-10.1.1.0-bin/lib/derby.jar
Non-English users should include an additional addpath directive in this file for the language specific locale jar of their choice
At this point, JBuilder itself will be aware of Derby, and will be able to use Derby. For example, if you use data-aware controls, like DBSwing, they will be able to function properly with the JBuilder design view.
org.apache.derby.jdbc.EmbeddedDriver
derby: databaseName ; URLAttributes
Note : do not include the usual “jdbc:” at the front of the URL, as it will be added automatically by Database Pilot.
create=true
jdbc:derby:/Users/lori/Databases/test;create=true
jdbc:derby:c:/databases/test;create=true
Note: There is a documented Derby bug on Mac OS X for creating databases as described above (create=true). I did not experience this problem, but YMMV.
While you could just move on to using Derby in your own projects now, if might help to take it for a test drive first. JBuilder's HelloJDBC sample was created as a demonstration of the embedded version of JDataStore. Which makes it a perfect candidate to adapt for use with embedded version of Derby.
We have already seen one way of creating a new Derby database using Database Pilot. Now we will create one in code.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; public class CreateDerbyDatabase { public static final String DERBY_EMBEDDED_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; public static final String DERBY_URL = "jdbc:derby:"; public static void main(String[] args) { String derbyFile = "/Users/lori/Databases/TEMPORARY"; String derbyOptions = ";create=true"; String user = "sysdba"; String passwd = "masterkey"; try { Class.forName(DERBY_EMBEDDED_DRIVER); } catch (ClassNotFoundException ex) { Logger.global.log(Level.SEVERE, "Derby JDBC driver not found in the classpath"); System.exit(1); } Connection con = null; try { con = DriverManager.getConnection(DERBY_URL + derbyFile + derbyOptions, user, passwd); Logger.global.log(Level.INFO, "Derby database successfully created"); } catch (SQLException ex) { Logger.global.log(Level.WARNING, "Derby database could not be created" + ex); } finally { if ( con != null ) { try { con.close(); } catch (SQLException ex) { // ignore this } } } } }
Sep 9, 2005 6:06:20 PM CreateDerbyDatabase main INFO: Derby database successfully created
That was easy. Now, let's modify one of the existing classes, HelloJDBC, to use Derby. Since we have already created the database that we will be using, we will not be using the URLAttributes for this URL.
// String DRIVER = "com.borland.datastore.jdbc.DataStoreDriver"; String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
// String URL = "jdbc:borland:dslocal:"; String URL = "jdbc:derby:";
// String FILE = "TEMPORARY.jds"; String FILE = "/Users/lori/Databases/TEMPORARY";
COLOR,NUMBER,PRICE, Red,1,7.99, Blue,2,8.99, Green,3,9.99,
Now that you see how easy it is, you should be ready to use Derby in one of your own JBuilder projects.
While I can make no special claim to being an Apache Derby expert, I've looked at Derby (aka Cloudscape), off and on for years, since it first appeared as part of the Sun J2EE reference implementation. It is nice to see that it has evolved into a viable open source project and I hope to see its' feature set continue to be expanded in the future. Meanwhile, we have an open source pure JavaTM database to play with in JBuilder. Enjoy!
Published on: 1/6/2006 12:00:00 AM
Server Response from: BDN9A
Borland® Copyright© 1994 - 2008 Borland Software Corporation. All rights reserved. Contact Us | Site Map | Legal Notices | Privacy Policy | Report Software Piracy