<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ActiveRecord-JDBC code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 23:10:08 GMT</pubDate>
    <description>DZone Snippets: ActiveRecord-JDBC code</description>
    <item>
      <title>JRuby + ActiveRecord-JDBC</title>
      <link>http://snippets.dzone.com/posts/show/4021</link>
      <description>Sample of using JRuby w/JDBC and activerecord&lt;br /&gt;&lt;br /&gt;db.sql&lt;br /&gt;&lt;code&gt;&lt;br /&gt;create database test;&lt;br /&gt;grant all privileges on test.* to 'test'@'localhost' identified by 'test' with grant option;&lt;br /&gt;drop table if exists tests;&lt;br /&gt;create table tests (&lt;br /&gt;  id          int          not null auto_increment,&lt;br /&gt;  name        varchar(100) not null,&lt;br /&gt;  description text         not null,&lt;br /&gt;  primary key (id)&lt;br /&gt;);&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;jruby_db_test.rb  &lt;br /&gt;&lt;code&gt;&lt;br /&gt;# JRuby ActiveRecord-JDBC active_record&lt;br /&gt;# jdbc drivers must be located in the CLASSPATH or in $JRUBY_HOME/lib&lt;br /&gt;require 'rubygems'&lt;br /&gt;gem 'ActiveRecord-JDBC'&lt;br /&gt;require 'jdbc_adapter'&lt;br /&gt;require 'active_record'&lt;br /&gt;require 'java'&lt;br /&gt;&lt;br /&gt;# Load DB configuration - yml file in same directory&lt;br /&gt;def connect()&lt;br /&gt;  conf = YAML::load(File.open('database.yml'))&lt;br /&gt;  ActiveRecord::Base.establish_connection(conf)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Test &lt; ActiveRecord::Base&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;connect&lt;br /&gt;t = Test.new&lt;br /&gt;t.name = 'test'&lt;br /&gt;t.save&lt;br /&gt;&lt;br /&gt;t1 = Test.find(1)&lt;br /&gt;puts t1.name&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;database.yml &lt;br /&gt;&lt;code&gt;&lt;br /&gt;adapter: jdbc&lt;br /&gt;driver: com.mysql.jdbc.Driver&lt;br /&gt;url: jdbc:mysql://localhost:3306/test&lt;br /&gt;username: test&lt;br /&gt;password: test&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 15 May 2007 06:05:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4021</guid>
      <author>sr_bean (Sr. Bean)</author>
    </item>
  </channel>
</rss>
