FaqRailsJRubyDatabaseAccess

How do I access databases from my JRuby Rails project?

Contents


Using Non-JDBC Adapters

The MySQL adapter is included with JRuby. You use this adapter just as you would with a native Ruby on Rails application.

For PosgreSQL, you can use the postgres-pr gem.

That is all I know about this, so if you have information to add, please do so.

Using Database-Specific JDBC Adapters

If you are using Rails 2.0 or later, you can use the database-specific JDBC adapters. At the time of this writing, the following adapters are available.

  • activerecord-jdbcderby-adapter
  • activerecord-jdbch2-adapter
  • activerecord-jdbcjhsqldb-adapter
  • activerecord-jdbcmysql-adapter
  • activerecord-jdbcpostgresql-adapter
  • activerecord-jdbcsqlite-adapter

These adapters take care of loading the JDBC code. They also load a copy of the database server's JDBC driver JAR file, so you do not need to download the JAR file or add it to your classpath.

To use these adapters, you use the same configuration for a Ruby project, except that you prepend jdbc to the adapter name, as shown in the following example.

development:
  adapter: jdbcpostgresql
  database: rubyweblog_development
  username: postgres
  password: mypassword
  host: localhost


Using the Generic JDBC Adapter

This original method for accessing databases through JDBC connections is still supported, and is the method that the 6.1 IDE uses when you check the Access Database Using JDBC checkbox during project creation. You can use this method if a database-specific adapter is not available for your database server.

To use the generic JDBC adapter, install the activerecord-jdbc-adapter gem and download the database server's JDBC 3.0 compliant driver JAR file. The JDBC driver must be a pure Java driver. Put the JAR file in your classpath.

You have two options for setting up your configuration. You can use the Rails standard configuration for your database server, just as you would with native Ruby, or you can explicitly specify the driver and URL.

Using the Standard Configuration

Here, you can use the same configuration as you would use for a native Ruby application. However, you must add the following statements to your environment.rb file, just before the Rails::Initializer line. This method is good to use if you intend to switch your project's platform from native Ruby to JRuby or the other way around.

 if RUBY_PLATFORM =~ /java/
   require 'rubygems'
   gem 'activerecord-jdbc-adapter'
   require 'arjdbc'
 end

With the above statements in your environment.rb file, you can use a configuration like the following example:

development:
  adapter: postgresql
  encoding: unicode
  database: rubyweblog_development
  username: postgres
  password: adminadmin
  host: localhost
  port: 5432


Using the JDBC Configuration

Here, you must supply the driver and URL in the database configurations in the database.yml file, as shown in the following example.

development:
  adapter: jdbc
  driver: org.postgresql.Driver
  url: jdbc:postgresql://localhost/rubyweblog_development
  encoding: unicode
  username: postgres
  password: mypassword
  host: localhost
  port: 5432

If you are using a Rails version that is earlier than 2.0, you must add the following statements to your environment.rb file, just before the Rails::Initializer line. You do not need these statements for Rails 2.0 and later.

 if RUBY_PLATFORM =~ /java/
   require 'rubygems'
   gem 'activerecord-jdbc-adapter'
   require 'arjdbc'
 end

Known Issues

  • The db:create rake task does not work with JDBC/Postgresql (JRUBY-2715)

For More Information



Applies to: NetBeans 6.0 and 6.1

Platforms: All

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo