2007/11/13

NetBeans - Mixing Java and Ruby Applications

Mixing Java and Ruby Applications
Contributed by Tor Norbye and maintained by Beth Stearns
October 2007 [Revision number: V6.0-1]
This publication is applicable to NetBeans IDE 6.0 release

This article shows how to combine Ruby and Java applications using the NetBeans IDE.

Contents

- Article Requirements
- Create the Java Project
- Edit the Java Project
- Write the Rails Application



Article Requirements


This article shows how to combine Java applications with Ruby applications. Along the way, it demonstrates some of the latest Ruby capabilities in the NetBeans IDE 6.0.

This article requires the following:

A basic knowledge of programming with the Ruby technologies
NetBeans IDE 6.0 with Ruby (download) on your compouter
Set Up Instructions
There are several set-up tasks, principally involving database set up and including TopLink JAR files. Here is how to add the required currency data to a database table. When you set up the Rails project, you will add the necessary TopLink JAR files.

You need to configure a database as follows:

Create a table named CURRENCY.
Create three string (or VARCHAR) columns in the CURRENCY table: Country, Currency, and Name.
Populate the table with some data. For example, you might add these three currencies plus any others you want: "USA", "Dollar", "USD"; "Czech Republic", "Koruna", "CZK"; "Norway", "Krone", "NOK".
You also must set up NetBeans so that it can access the database table. That is, if need be, configure a JDBC driver for the database and add a data source in the Services window.

For example, the following SQL creates a CURRENCY table in a Derby database called SAMPLE (with a username and password combination of APP/APP). If you want to use the same SAMPLE database, you can execute this SQL from within the IDE (after connecting to the SAMPLE database) and create the table. Feel free to create your own Derby database, too, using the Tools->Java DB Database->Create Database action. The SQL to create the same table on a different database system may differ from this example.

drop table "APP"."CURRENCY";
create table "APP"."CURRENCY" (
country VARCHAR(20), currency VARCHAR(20), name VARCHAR (20),
id INTEGER GENERATED always AS IDENTITY);

alter table CURRENCY add constraint currencyPK PRIMARY KEY (id);
INSERT INTO CURRENCY VALUES ('USA', 'Dollar', 'USD',DEFAULT);
INSERT INTO CURRENCY VALUES ('Czech Republic', 'Koruna', 'CZK',DEFAULT);
INSERT INTO CURRENCY VALUES ('Norway', 'Krone', 'NOK',DEFAULT);
INSERT INTO CURRENCY VALUES ('France', 'Euro', 'EU',DEFAULT);
Create the Java Project
Once you have completed the setup tasks, you can create your project. Begin by creating a Java Desktop application, which is really a Swing application, and choose the option for database binding. The wizard takes you through the steps to connect the project, which we call Money, to the datasource, which in our case is the CURRENCY table on the Derby sample database. In a few button clicks you have a fully functional database CRUD application.

No comments: