Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Two ways to build Apache Derby database applications in JRuby

Fuse the power of Ruby with Derby

Robert Thornton (ret1@acm.org), Java Consultant, Daugherty Business Solutions
Robert Thornton has been developing in the Java language for six years and is currently a consultant for Daugherty Business Solutions. He works as a developer and architect on Java EE applications at various clients in the St. Louis, Mo., area. He has a strong focus on object-oriented development and works primarily in the Java and C++ languages.

Summary:  JRuby is an implementation of the Ruby programming language that runs in the Java™ Virtual Machine (JVM). This tutorial demonstrates how to install and configure JRuby to connect to the Apache Derby database. As Ruby and Ruby on Rails become more popular, JRuby becomes an interesting technology to leverage the portability, security, and class libraries of the JVM while using Ruby features like simple syntax and closures. If you're writing a small application that needs a relational database, or you want to be able to ship a database with your product, then the Derby database is an excellent choice for a persistence mechanism. This tutorial shows you two methods of connecting to and building Derby database applications from JRuby.

Date:  30 Jan 2007
Level:  Intermediate PDF:  A4 and Letter (68 KB | 19 pages)Get Adobe® Reader®

Activity:  11854 views
Comments:  

Before you start

About this tutorial

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.


Objectives

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:

  1. Using JDBC via Ruby within JRuby
  2. 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)

Prerequisites

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.


System requirements

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.

1 of 9 | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Information Management, Java technology
ArticleID=193144
TutorialTitle=Two ways to build Apache Derby database applications in JRuby
publish-date=01302007
author1-email=ret1@acm.org
author1-email-cc=ruterbo@us.ibm.com