tags:

views:

1964

answers:

4

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,

A: 

Have you got the Oracle client installed? you probably need at least the jdbc driver files from the client

Colin Pickard
Yes, but I did not think it would be required with this "thin" connection type.
Rob
+1  A: 

It turns out that my ojdbc14.jar file was corrupt.

Further, the jar file MUST be in the jruby/lib directory. Simply having it on the classpath does not work.

Rob
A: 

require 'java'

This require doesn't load the jdbc driver jar into the system class path

require "c:/ruby/jruby-1.2.0/lib/ojdbc14.jar"

2 ways you can load the class (There are probably more)

1 ruby syntax for java class name

Java::OracleJdbcDriver::OracleDriver

2 Use the thread context class loader

java.lang.Class.forName("oracle.jdbc.driver.OracleDriver", true, java.lang.Thread.currentThread.getContextClassLoader)

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

Bryan Castillo
A: 

Ummm I'm a little confused. Tried this myself today with JRuby 1.5.3 and Oracle ojdbc6.jar JVM(IBM Java 6) and I'm getting

NativeException: java.sql.SQLException: No suitable driver
from java/sql/DriverManager.java:320:in 'getConnection'
from java/sql/DriverManager.java:348:in 'getConnection'

However, I've set everything up just as OP listed in his post and I'm getting nothing. I was confused about Brian Castillo's answer although I tried that as well. Anyone, shed some light on this question again even though its a bit old?

I'm not working at an Oracle site any more (hooray!), but I would suggest trying the activerecord-jdbc-adapter gem - it's quite actively developed. http://github.com/nicksieger/activerecord-jdbc-adapter
Rob