DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
JRuby + ActiveRecord-JDBC
Sample of using JRuby w/JDBC and activerecord
db.sql
create database test; grant all privileges on test.* to 'test'@'localhost' identified by 'test' with grant option; drop table if exists tests; create table tests ( id int not null auto_increment, name varchar(100) not null, description text not null, primary key (id) );
jruby_db_test.rb
# JRuby ActiveRecord-JDBC active_record
# jdbc drivers must be located in the CLASSPATH or in $JRUBY_HOME/lib
require 'rubygems'
gem 'ActiveRecord-JDBC'
require 'jdbc_adapter'
require 'active_record'
require 'java'
# Load DB configuration - yml file in same directory
def connect()
conf = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(conf)
end
class Test < ActiveRecord::Base
end
connect
t = Test.new
t.name = 'test'
t.save
t1 = Test.find(1)
puts t1.name
database.yml
adapter: jdbc driver: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test username: test password: test




