Before you start
Apache's Derby database is a small, in-memory database that's ideal for use as an embedded database. (A link to a thorough introduction to Derby is listed in the Resources section.)
JRuby is a pure-Java implementation of the Ruby programming language. Ruby is an object-oriented scripting language well suited for purposes as diverse as system-management scripts, rich Internet applications, or desktop applications. JRuby is currently at a 0.9 release (at the time of this writing), so it's getting close to covering all the functionality in the standard C-based Ruby interpreter. There are still limitations you need to be aware of if you put JRuby into production, but it works for most uses. The two lead developers of the JRuby project were recently hired by Sun, so you can look forward to continued momentum on the JRuby project in the future. Because JRuby lives inside the JVM, you can use the Java class library and do things like implement Java interfaces in Ruby.
This tutorial covers some JRuby basics and how to work with Derby. A developerWorks introduction to JRuby, which describes many of the features of JRuby as of 2004 (version 0.7), and other JRuby tutorials are listed in the Resources section.
The goal of this tutorial is to show you how to connect to and use the Apache Derby database via the JRuby interpreter. You'll learn how to insert data into and retrieve it from Derby via JRuby. Two methods of interacting with Derby are presented:
- Using JDBC via Ruby within JRuby
- Using Ruby's ActiveRecord library (via the ActiveRecord-JDBC library), which has been developed as a part of Ruby on Rails, but can be used stand-alone for Object-Relational Mapping (ORM)
This tutorial is written for developers who are familiar with Java code and JDBC. Experience with Ruby isn't necessary, but exposure to a scripting language (Perl, Python, Ruby, and so on) will make the syntax easier to follow. You don't need any specific integrated development environment (IDE) or tool knowledge. Familiarity with a text editor and setting environment variables is required.
To run the examples in this tutorial, you must have a system capable of running the JVM. The tutorial assumes that your Java environment is successfully configured, so download and install the JVM before continuing with this tutorial.
At this time, you need to be able to run Subversion from the command line to complete the ActiveRecord portion of the tutorial. Download a Subversion client (see the Resources section for links) before continuing with this tutorial if you wish to perform the ActiveRecord examples.