How to connect to Oracle using JRuby & JDBC

Posted by Rob on Stack Overflow See other posts from Stack Overflow or by Rob
Published on 2009-04-16T08:22:24Z Indexed on 2010/06/03 5:44 UTC
Read the original article Hit count: 272

Filed under:
|
|

First approach: bare metal

require 'java'
require 'rubygems'
require "c:/ruby/jruby-1.2.0/lib/ojdbc14.jar"  # should be redundant, but tried it anyway
odriver = Java::JavaClass.for_name("oracle.jdbc.driver.OracleDriver")
puts odriver.java_class
url = "jdbc:oracle:thin:@myhost:1521:mydb"
puts "About to connect..."
con = java.sql.DriverManager.getConnection(url, "myuser", "mypassword");
if con
    puts " connection good"
else
    puts " connection failed"
end

The result of the above is:

sqltest.rb:4: cannot load Java class oracle.jdbc.driver.OracleDriver (NameError)

Second approach: Active Record

require 'rubygems'
gem 'ActiveRecord-JDBC'
require 'jdbc_adapter'
require 'active_record'
require 'active_record/version'
require "c:/ruby/jruby-1.2.0/lib/ojdbc14.jar"  # should be redundant...

ActiveRecord::Base.establish_connection(
   :adapter => 'jdbc',
   :driver => 'oracle.jdbc.driver.OracleDriver',
   :url => 'jdbc:oracle:thin:@myhost:1521:mydb',
   :username=>'myuser',
   :password=>'mypassword'
 )
ActiveRecord::Base.connection.execute("SELECT * FROM mytable")

The result of this is:

C:/ruby/jruby-1.2.0/lib/ruby/gems/1.8/gems/activerecord-jdbc-adapter-0.9.1/lib/active_recordconnection_adapters/jdbc_adapter.rb:330:in `initialize': 
The driver encountered an error: cannot load Java class oracle.jdbc.driver.OracleDriver (RuntimeError)

Essentially the same error no matter how I go about it.

I'm using JRuby 1.2.0 and I have ojdbc14.jar in my JRuby lib directory

Gems:

  • ActiveRecord-JDBC (0.5)
  • activerecord-jdbc-adapter (0.9.1)
  • activerecord (2.2.2)

What am I missing?

Thanks,

© Stack Overflow or respective owner

Related posts about Oracle

Related posts about jdbc