Two Methods for building Apache Derby database applications in JRuby (1)

Source: Internet
Author: User
Tags subversion client

Robert Thornton has six years of Java language development experience and is currently a consultant for Daugherty Business Solutions. He served in St. louis, Mo ., developers and architects who use Java EE applications on various clients in the field. They are good at object-oriented development and mainly use Java and C ++ at work.

JRuby is running in Java™Virtual Machine Java™Implementation of the Ruby programming language in 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 increasingly popular, JRuby has become a widely used technology, use JVM portability, security, and class libraries when using Ruby features such as simple syntax and closures. If you write a small application that requires a relational database, or you need to be able to deliver the database with the product, the Derby database is the best choice for the persistence mechanism. This tutorial will show you two ways to connect to JRuby and build a Derby database application from JRuby.

Before getting started

About this tutorial

Apache Derby database is a small in-memory database, which is ideal for Embedded databases. The references section lists the complete Derby introduction links .)

JRuby is a pure Java implementation of the Ruby programming language. Ruby is an object-oriented scripting language applicable to various system management scripts, rich Internet applications, or desktop applications. The current version of JRuby is JRuby 0.9 when writing this article), so it covers almost all the functions in the Standard C-based Ruby parser. If you want to use JRuby for production, there are still some restrictions that need attention, but JRuby can be used normally in most cases. Two senior developers of the JRuby project have recently been hired by Sun, so you can expect the development momentum of the JRuby project to continue in the future. Because JRuby is located in JVM, you can use Java class libraries and perform operations such as Java interfaces in Ruby.

This tutorial introduces some basic knowledge about JRuby and how to use Derby. The reference section lists the introductory articles on JRuby and other JRuby tutorials on developerWorks, which describe many of the features of JRuby 2004 as of 0.7.

Purpose

The purpose of this tutorial is to demonstrate how to connect to and use the Apache Derby database through the JRuby parser. You will learn how to insert data into Derby through JRuby and how to retrieve data from Derby. The following two methods are provided for interaction with Derby:

  1. Use JDBC in JRuby
  2. Using the ActiveRecord library of Ruby through ActiveRecord-JDBC Library), this library was developed as part of Ruby on Rails, but can be used independently for Object-Relational Mapping, ORM)

Prerequisites

This tutorial applies to developers familiar with Java code and JDBC. You do not need to have Ruby experience, but understand the scripting languages Perl, Python, Ruby, and so on. You do not need any specific integrated development environment (IDE) or tool knowledge. Be familiar with text editors and set environment variables.

System Requirements

To run the examples in this tutorial, the system must be able to run JVM. This tutorial assumes that the Java environment has been successfully configured, so you need to download and install JVM first, and then continue to learn this tutorial.

This time, you must be able to run Subversion from the command line to complete the ActiveRecord section of this tutorial. If you need to execute the ActiveRecord example, You need to download the Subversion client Link first. For more information, see references.

Getting started with JRuby

Install JRuby and write the first JRuby script.

Install JRuby

Installing JRuby is a simple process, including Decompression and distribution and setting environment variables.

  1. Download JRuby from the JRuby homepage, see references), and decompress it to the selected directory.
  2. Set the JRUBY_HOME environment variable to the directory to which JRuby is extracted. For example,set JRUBY_HOME=c:\java\jruby.
  3. FinallyJRUBY_HOMEAdd it to the PATH environment variable.

Compile a simple JRuby script

Now, compile a simple JRuby script to check whether the environment is correctly configured and view the Ruby and Java features of JRuby for the first time.

  1. Open the text editor and create a file containing the following code:puts 'Hello World'

In Ruby,putsIs a method for printing strings. Think of itOutput string. This line of code is equivalent to the following line of Java code:System.out.println("Hello World"). For more information about Ruby, see [developerWorks, August December 2005].

  1. Save the file as helloWorld. rb and return it to the command line. Running JRuby on this script will display the following results:

jruby helloWorld.rb

     Hello World

The result shows that JRuby has successfully activated JVM and executed the Ruby script. The next step is to ensure that you can access the Java class and connect to Derby using JDBC.

  1. Add the three lines shown in Listing 1 to the helloWorld. rb file:

Listing 1: ensuring access to Java classes

require 'java'
include_class("java.lang.System")
System.out.println("Hello Java World")

When calling a Java class from a JRuby script, you need to explicitly tell JRuby which classes or software packages will be referenced. Suchinclude_classAnd other JRuby methods. To import those JRuby methods into the script, use the first line above.require 'java'). SetrequireConsideredimportRuby equivalent command. Because you need to access a classjava.lang.SystemTherefore, you need to inform JRuby of the operations to be performed. This class can be referenced on it.out.println. The last line can be copied directly from the Java program.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.