<?xml version="1.0" encoding="UTF-8"?>
<page>
  <created-at type="datetime">2009-03-22T17:11:29Z</created-at>
  <description>Added username/password line for Derby (it doesn't work without them)</description>
  <id type="integer">1111</id>
  <name>ActiveRecord-JDBC</name>
  <number type="integer">5</number>
  <person-id type="integer">41425</person-id>
  <text>[[Home|&amp;raquo; JRuby Project Wiki Home Page]]
&lt;h1&gt;ActiveRecord-JDBC Database Support&lt;/h1&gt;
ActiveRecord-JDBC 0.9.1 supports normal AR operations and basic migrations for the following databases:
* MySQL
* PostgreSQL
* SQLite3 (except &lt;tt&gt;change_column&lt;/tt&gt;)
* Oracle
* HSQLDB (except &lt;tt&gt;migrations&lt;/tt&gt; and &lt;tt&gt;habtm&lt;/tt&gt; at least.)
* H2
* Microsoft SQL Server (except for &lt;tt&gt;change_column_default&lt;/tt&gt;)
* DB2 (except &lt;tt&gt;change_column&lt;/tt&gt;, &lt;tt&gt;change_column_default&lt;/tt&gt;, &lt;tt&gt;rename_column&lt;/tt&gt;, &lt;tt&gt;remove_column&lt;/tt&gt;, &lt;tt&gt;add_index&lt;/tt&gt;, &lt;tt&gt;remove_index&lt;/tt&gt;, and &lt;tt&gt;rename_table&lt;/tt&gt;)
* Derby (except &lt;tt&gt;change_column&lt;/tt&gt;, &lt;tt&gt;change_column_default&lt;/tt&gt;, &lt;tt&gt;remove_column&lt;/tt&gt;, &lt;tt&gt;rename_column&lt;/tt&gt;)
* FireBird (except &lt;tt&gt;change_column_default&lt;/tt&gt; and &lt;tt&gt;rename_column&lt;/tt&gt;)

==Usage==
Here's a walkthrough for MySQL in ultra-condensed form:

'''1.''' Grab a JRuby release from [http://jruby.org/download JRuby Downloads] and unpack it

'''2.''' Set up path for JRuby

      export PATH=$PATH:[jruby-dir]/bin

'''3.''' Install Rails

      jruby -S gem install rails

'''4.''' Install ActiveRecord-JDBC for MySQL (see [http://jruby-extras.rubyforge.org/activerecord-jdbc-adapter the documentation] for other options). This will also install the dependent gems.

      jruby -S gem install activerecord-jdbcmysql-adapter

:You should see something like the following:

      Successfully installed activerecord-jdbc-adapter-0.9.2
      Successfully installed jdbc-mysql-5.0.4
      Successfully installed activerecord-jdbcmysql-adapter-0.9.2

'''5.''' Generate a Rails App

      rails ~/testapp --database mysql

'''6.''' Go to the app

      cd ~/testapp

'''7.''' If you're using Rails 2.0 modify &lt;tt&gt;database.yml&lt;/tt&gt; by prepending &lt;tt&gt;jdbc&lt;/tt&gt; to the adapter name. For PostgreSQL, you'll need to add the &lt;tt&gt;host&lt;/tt&gt; parameter as well. If you're NOT using Rails 2.0, don't modify your &lt;tt&gt;database.yml&lt;/tt&gt; file.

'''ActiveRecord-JDBC version 0.9.0:'''
      #SQLite3
      development:
        adapter: jdbcsqlite3
        url: jdbc:sqlite:test.db # path to sqlite3 dbfile

'''ActiveRecord-JDBC version 0.9.1:'''
      #SQLite3
      development:
        adapter: jdbcsqlite3
        database: db/development.db

      #MYSQL
      development:
        adapter: jdbcmysql
        encoding: utf8
        database: testapp_development
        username: root
        password:

      #POSTGRES
      development:
        adapter: jdbcpostgresql
        encoding: unicode
        host: localhost
        database: testapp_development
        username: testapp
        password:

      #ORACLE
      development:
        adapter: jdbc
        driver: oracle.jdbc.OracleDriver
        url: jdbc:oracle:thin:@myOracleHost:1521:mySID
        username: myUser
        password: myPass

      #JavaDB/DerbyDB
      development:
        adapter: jdbc
        driver: org.apache.derby.jdbc.EmbeddedDriver
        url: jdbc:derby:{database};create=true
        username: myUser
        password: myPass
        

'''8.''' If you're using Rails 2.0, you're done. The rest of configuration, including the JDBC driver jar, is done automatically.  If you're running Rails 1.2.x, you'll need to modify &lt;tt&gt;environment.rb&lt;/tt&gt;. Add the following snippet inside the &lt;tt&gt;Rails::Initializer&lt;/tt&gt; block:

      if RUBY_PLATFORM =~ /java/
        require 'jdbc/mysql' #per jdbc-mysql-5.0.4/README.txt
      end

'''9.''' Create a &lt;tt&gt;testapp_development&lt;/tt&gt; database, grants for &lt;tt&gt;testapp&lt;/tt&gt; user, and a &lt;tt&gt;widgets&lt;/tt&gt; table in MySQL (use migrations if you like).

'''10.''' Scaffold Widgets CRUD

      jruby script/generate scaffold widget

'''11.''' Start up the server

      jruby script/server

'''12.''' In your browser, go to [http://localhost:3000/widgets].



And that's about it. You've now got a scaffolded widget page running in JRuby over JDBC to MySQL.

==Troubleshooting==
* '''Scaffold fails with an error about &quot;nonexistent jdbc adapter&quot;:''' Ensure that you've added the &lt;tt&gt;require&lt;/tt&gt; line to &lt;tt&gt;environment.rb&lt;/tt&gt;.
* '''Scaffold and script/server terminate without running:''' Make sure you've successfully installed the activerecord-jdbc-adapter gem. The additional &lt;tt&gt;require&lt;/tt&gt; in &lt;tt&gt;environment.rb&lt;/tt&gt; causes Rails scripts to die silently if there are any errors.
* '''Scaffold fails with the error &quot;cannot convert NilClass into String&quot;:''' Make sure you've correctly specified the driver and url lines in &lt;tt&gt;database.yml&lt;/tt&gt;
* '''You see the following error when using SQL Server:'''&lt;br/&gt;&lt;tt&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;com.microsoft.sqlserver.jdbc.AuthenticationJNI&lt;br/&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{clinit}&lt;br/&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;WARNING: Failed to load the sqljdbc_auth.dll&lt;/tt&gt;&lt;br/&gt;See [[FAQs#troubleshooting_JNI|I am having a weird JNI problem. Help me!]]
* '''You have foxy_fixtures plugin installed and you get the following error:'''&lt;br/&gt;&lt;tt&gt; &amp;nbsp;'load_missing_constant':&lt;br/&gt; &amp;nbsp; uninitialized constant ActiveRecord::ConnectionAdapters::MysqlAdapter (NameError)&lt;/tt&gt;&lt;br/&gt;This error occurs because foxy_features ships with built-in active_record adapters for MySQL, SQLite and PostgreSQL. Specifying any of the &lt;tt&gt;jdbc:{dbname}&lt;/tt&gt;'s, or just &lt;tt&gt;jdbc&lt;/tt&gt;, will cause foxy_fixtures to load the MySQL Ruby driver by default. This, obviously, will fail since there is no native MySQL driver for JRuby. To be able to run the app, the easy fix is to take the plugin out&amp;mdash;uninstall it. Development and testing with foxy_fixtures and &lt;tt&gt;activerecord-jdbc&lt;/tt&gt; still needs to be researched.
</text>
  <text-as-html>&lt;p&gt;&lt;a href='&lt;?url_for_page Home?&gt;' class='&lt;?class_for_page_link Home?&gt;'&gt;&amp;raquo; JRuby Project Wiki Home Page&lt;/a&gt;&lt;h1&gt;ActiveRecord-JDBC Database Support&lt;/h1&gt;
ActiveRecord-JDBC 0.9.1 supports normal AR operations and basic migrations for the following databases:
&lt;/p&gt;&lt;ul&gt;&lt;li&gt; MySQL
&lt;/li&gt;&lt;li&gt; PostgreSQL
&lt;/li&gt;&lt;li&gt; SQLite3 (except &lt;tt&gt;change_column&lt;/tt&gt;)
&lt;/li&gt;&lt;li&gt; Oracle
&lt;/li&gt;&lt;li&gt; HSQLDB (except &lt;tt&gt;migrations&lt;/tt&gt; and &lt;tt&gt;habtm&lt;/tt&gt; at least.)
&lt;/li&gt;&lt;li&gt; H2
&lt;/li&gt;&lt;li&gt; Microsoft SQL Server (except for &lt;tt&gt;change_column_default&lt;/tt&gt;)
&lt;/li&gt;&lt;li&gt; DB2 (except &lt;tt&gt;change_column&lt;/tt&gt;, &lt;tt&gt;change_column_default&lt;/tt&gt;, &lt;tt&gt;rename_column&lt;/tt&gt;, &lt;tt&gt;remove_column&lt;/tt&gt;, &lt;tt&gt;add_index&lt;/tt&gt;, &lt;tt&gt;remove_index&lt;/tt&gt;, and &lt;tt&gt;rename_table&lt;/tt&gt;)
&lt;/li&gt;&lt;li&gt; Derby (except &lt;tt&gt;change_column&lt;/tt&gt;, &lt;tt&gt;change_column_default&lt;/tt&gt;, &lt;tt&gt;remove_column&lt;/tt&gt;, &lt;tt&gt;rename_column&lt;/tt&gt;)
&lt;/li&gt;&lt;li&gt; FireBird (except &lt;tt&gt;change_column_default&lt;/tt&gt; and &lt;tt&gt;rename_column&lt;/tt&gt;)
&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;&lt;a name='Usage'&gt;&lt;/a&gt;Usage&lt;/h2&gt;
&lt;p&gt;Here's a walkthrough for MySQL in ultra-condensed form:

&lt;/p&gt;&lt;p&gt;&lt;b&gt;1.&lt;/b&gt; Grab a JRuby release from &lt;a class='external' href=&quot;http://jruby.org/download&quot;&gt;JRuby Downloads&lt;/a&gt; and unpack it

&lt;/p&gt;&lt;p&gt;&lt;b&gt;2.&lt;/b&gt; Set up path for JRuby

&lt;/p&gt;&lt;pre&gt;      export PATH=$PATH:[jruby-dir]/bin
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;3.&lt;/b&gt; Install Rails

&lt;/p&gt;&lt;pre&gt;      jruby -S gem install rails
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;4.&lt;/b&gt; Install ActiveRecord-JDBC for MySQL (see &lt;a class='external' href=&quot;http://jruby-extras.rubyforge.org/activerecord-jdbc-adapter&quot;&gt;the documentation&lt;/a&gt; for other options). This will also install the dependent gems.

&lt;/p&gt;&lt;pre&gt;      jruby -S gem install activerecord-jdbcmysql-adapter
&lt;/pre&gt;&lt;dl&gt;&lt;dd&gt;You should see something like the following&lt;/dd&gt;&lt;dd&gt;&lt;/dd&gt;&lt;/dl&gt;&lt;pre&gt;      Successfully installed activerecord-jdbc-adapter-0.9.2
      Successfully installed jdbc-mysql-5.0.4
      Successfully installed activerecord-jdbcmysql-adapter-0.9.2
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;5.&lt;/b&gt; Generate a Rails App

&lt;/p&gt;&lt;pre&gt;      rails ~/testapp --database mysql
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;6.&lt;/b&gt; Go to the app

&lt;/p&gt;&lt;pre&gt;      cd ~/testapp
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;7.&lt;/b&gt; If you're using Rails 2.0 modify &lt;tt&gt;database.yml&lt;/tt&gt; by prepending &lt;tt&gt;jdbc&lt;/tt&gt; to the adapter name. For PostgreSQL, you'll need to add the &lt;tt&gt;host&lt;/tt&gt; parameter as well. If you're NOT using Rails 2.0, don't modify your &lt;tt&gt;database.yml&lt;/tt&gt; file.

&lt;/p&gt;&lt;p&gt;&lt;b&gt;ActiveRecord-JDBC version 0.9.0:&lt;/b&gt;&lt;/p&gt;&lt;pre&gt;      #SQLite3
      development:
        adapter: jdbcsqlite3
        url: jdbc:sqlite:test.db # path to sqlite3 dbfile
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;ActiveRecord-JDBC version 0.9.1:&lt;/b&gt;&lt;/p&gt;&lt;pre&gt;      #SQLite3
      development:
        adapter: jdbcsqlite3
        database: db/development.db
&lt;/pre&gt;&lt;pre&gt;      #MYSQL
      development:
        adapter: jdbcmysql
        encoding: utf8
        database: testapp_development
        username: root
        password:
&lt;/pre&gt;&lt;pre&gt;      #POSTGRES
      development:
        adapter: jdbcpostgresql
        encoding: unicode
        host: localhost
        database: testapp_development
        username: testapp
        password:
&lt;/pre&gt;&lt;pre&gt;      #ORACLE
      development:
        adapter: jdbc
        driver: oracle.jdbc.OracleDriver
        url: jdbc:oracle:thin:@myOracleHost:1521:mySID
        username: myUser
        password: myPass
&lt;/pre&gt;&lt;pre&gt;      #JavaDB/DerbyDB
      development:
        adapter: jdbc
        driver: org.apache.derby.jdbc.EmbeddedDriver
        url: jdbc:derby:{database};create=true
        username: myUser
        password: myPass
        
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;8.&lt;/b&gt; If you're using Rails 2.0, you're done. The rest of configuration, including the JDBC driver jar, is done automatically.  If you're running Rails 1.2.x, you'll need to modify &lt;tt&gt;environment.rb&lt;/tt&gt;. Add the following snippet inside the &lt;tt&gt;Rails::Initializer&lt;/tt&gt; block:

&lt;/p&gt;&lt;pre&gt;      if RUBY_PLATFORM =~ /java/
        require 'jdbc/mysql' #per jdbc-mysql-5.0.4/README.txt
      end
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;9.&lt;/b&gt; Create a &lt;tt&gt;testapp_development&lt;/tt&gt; database, grants for &lt;tt&gt;testapp&lt;/tt&gt; user, and a &lt;tt&gt;widgets&lt;/tt&gt; table in MySQL (use migrations if you like).

&lt;/p&gt;&lt;p&gt;&lt;b&gt;10.&lt;/b&gt; Scaffold Widgets CRUD

&lt;/p&gt;&lt;pre&gt;      jruby script/generate scaffold widget
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;11.&lt;/b&gt; Start up the server

&lt;/p&gt;&lt;pre&gt;      jruby script/server
&lt;/pre&gt;&lt;p&gt;&lt;b&gt;12.&lt;/b&gt; In your browser, go to &lt;a class='external' href=&quot;http://localhost:3000/widgets&quot;&gt;http://localhost:3000/widgets&lt;/a&gt;.

&lt;/p&gt;&lt;p&gt;&lt;br /&gt;

&lt;/p&gt;&lt;p&gt;And that's about it. You've now got a scaffolded widget page running in JRuby over JDBC to MySQL.

&lt;/p&gt;&lt;h2&gt;&lt;a name='Troubleshooting'&gt;&lt;/a&gt;Troubleshooting&lt;/h2&gt;
&lt;ul&gt;&lt;li&gt; &lt;b&gt;Scaffold fails with an error about &amp;quot;nonexistent jdbc adapter&amp;quot;:&lt;/b&gt; Ensure that you've added the &lt;tt&gt;require&lt;/tt&gt; line to &lt;tt&gt;environment.rb&lt;/tt&gt;.
&lt;/li&gt;&lt;li&gt; &lt;b&gt;Scaffold and script/server terminate without running:&lt;/b&gt; Make sure you've successfully installed the activerecord-jdbc-adapter gem. The additional &lt;tt&gt;require&lt;/tt&gt; in &lt;tt&gt;environment.rb&lt;/tt&gt; causes Rails scripts to die silently if there are any errors.
&lt;/li&gt;&lt;li&gt; &lt;b&gt;Scaffold fails with the error &amp;quot;cannot convert NilClass into String&amp;quot;:&lt;/b&gt; Make sure you've correctly specified the driver and url lines in &lt;tt&gt;database.yml&lt;/tt&gt;&lt;/li&gt;&lt;li&gt; &lt;b&gt;You see the following error when using SQL Server:&lt;/b&gt;&lt;br /&gt;&lt;tt&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;com.microsoft.sqlserver.jdbc.AuthenticationJNI&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{clinit}&lt;br /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;WARNING: Failed to load the sqljdbc_auth.dll&lt;/tt&gt;&lt;br /&gt;See &lt;a href='&lt;?url_for_page FAQs?&gt;#troubleshooting_JNI' class='&lt;?class_for_page_link FAQs?&gt;'&gt;I am having a weird JNI problem. Help me!&lt;/a&gt;&lt;/li&gt;&lt;li&gt; &lt;b&gt;You have foxy_fixtures plugin installed and you get the following error:&lt;/b&gt;&lt;br /&gt;&lt;tt&gt; &amp;nbsp;'load_missing_constant':&lt;br /&gt; &amp;nbsp; uninitialized constant ActiveRecord::ConnectionAdapters::MysqlAdapter (NameError)&lt;/tt&gt;&lt;br /&gt;This error occurs because foxy_features ships with built-in active_record adapters for MySQL, SQLite and PostgreSQL. Specifying any of the &lt;tt&gt;jdbc:{dbname}&lt;/tt&gt;'s, or just &lt;tt&gt;jdbc&lt;/tt&gt;, will cause foxy_fixtures to load the MySQL Ruby driver by default. This, obviously, will fail since there is no native MySQL driver for JRuby. To be able to run the app, the easy fix is to take the plugin out&amp;mdash;uninstall it. Development and testing with foxy_fixtures and &lt;tt&gt;activerecord-jdbc&lt;/tt&gt; still needs to be researched.
&lt;/li&gt;&lt;/ul&gt;</text-as-html>
  <updated-at type="datetime">2010-01-27T23:44:25Z</updated-at>
  <wiki-id type="integer">320</wiki-id>
</page>
