Having problems accessing Oracle

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.

Gems:

ActiveRecord-JDBC (0.5)

activerecord-jdbc-adapter (0.9.1)

activerecord (2.2.2)

What am I missiing?

Thanks,

Hi,

I have been able to configure an Oracle connection via Active Record. I
can connect and read data, but I am having problems with date and time
fields.

However, you should find the code in my thread useful to see a basic
config that works for me.

See: Active record - Oracle DATE/TIMESTAMP issue - JRuby - Ruby-Forum

If you do this, can you please reply to my thread and let me know how
you get on with date/time fields?

Many Thanks

Adrian

Robert B. wrote:

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.

Gems:

ActiveRecord-JDBC (0.5)

activerecord-jdbc-adapter (0.9.1)

activerecord (2.2.2)

What am I missiing?

Thanks,

Try changing this
…for_name(“oracle.jdbc.driver.OracleDriver”)
to
for_name(“oracle.jdbc.driver.OracleDriver”, false,
JRuby.runtime.jruby_class_loader)

The jvm class path does not get the jruby required jars, so you have
to use a class loader that can. Hope that helps.

On Wed, Apr 15, 2009 at 7:27 PM, rob08 [email protected] wrote:

con = java.sql.DriverManager.getConnection(url, “myuser”, “mypassword”);

:adapter => ‘jdbc’,
`initialize’:
What am I missiing?

Thanks,


View this message in context: Having problems accessing Oracle
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

In my experience, this hasn’t worked with out-of-the-box JRuby since
-Xbootclasspath was added to the jruby.bat startup script back in 1.1.2.
Whether the jar is specified externally in a CLASSPATH env variable or
on
the command line, or internally via “require”, and whether the class is
loaded in the normal manner (which in JRuby is merely to reference it,
e.g.,
Java::oracle.jdbc.driver.OracleDriver) or by specifying either the JRuby
or
System classloader explicitly, *the driver does not see the correct
java.sql.DriverManager with which to register itself upon loading. *

There are two (and-a-half) workarounds that I know of. (There may be
others, but I have minimal patience for screwing around with this sort
of
configuration stuff. Anyone with more/better approaches please jump
in.)

  1. In jruby.bat, remove -Xbootclasspath, prepending the JRUBY_CP to the
    main
    classpath, i.e.:

    before: … -Xbootclasspath/a:"%JRUBY_CP%" -classpath
    “%CP%;%CLASSPATH%” …

    after: … -classpath “%JRUBY_CP%;%CP%;%CLASSPATH%” …

In other words, go back to the pre-1.1.2 startup method. However,
you’ll
lose the benefit of the improved startup speed that -Xbootclasspath
provides.

  1. Add the JDBC driver jar(s) (e.g. ojdbc14.jar) to -Xbootclasspath.
    This
    preserves the improvement in JRuby startup time.

2.5 Copy the JDBC driver jars to /path/to/jruby/lib. They’ll be added
to
-Xbootclasspath.

None of these are ideal in that you need to manually tweak the standard
JRuby installation, and will have to remember to do so whenever you
upgrade
either JRuby or your drivers.

BTW, you can check which drivers are registered using:

java.sql.DriverManager.drivers.to_a

-Bill

johnny P-2 wrote:

Try changing this
…for_name(“oracle.jdbc.driver.OracleDriver”)
to
for_name(“oracle.jdbc.driver.OracleDriver”, false,
JRuby.runtime.jruby_class_loader)

The jvm class path does not get the jruby required jars, so you have
to use a class loader that can. Hope that helps.

Johnny, thanks for your response.

I tried your suggestion but now I get:

sqltest.rb:14: wrong # of arguments(3 for 1) (ArgumentError)

It seems that JRuby cannot for the overload…


View this message in context:
http://www.nabble.com/Having-problems-accessing-Oracle-tp23070394p23088562.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks Bill,

I tried suggestion 1: made no difference - cannot load Java class
oracle.jdbc.driver.OracleDriver (NameError)

Suggestion 2: I attempted this (I think) by including ojdbc*.jar in the
call
to setjrubycp in _jrubyvars.bat. Made no difference.

Suggestion 2.5: It’s already there.

Bill D. wrote:

java.sql.DriverManager with which to register itself upon loading. *
“%CP%;%CLASSPATH%” …
2.5 Copy the JDBC driver jars to /path/to/jruby/lib. They’ll be added to

-Bill


View this message in context:
http://www.nabble.com/Having-problems-accessing-Oracle-tp23070394p23089710.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Bah! After all this time it looks like my ojdbc14.jar file is corrupt!

Stupid Oracle web site…

:-U

View this message in context:
http://www.nabble.com/Having-problems-accessing-Oracle-tp23070394p23094079.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

rob08 wrote:

Bah! After all this time it looks like my ojdbc14.jar file is corrupt!

Stupid Oracle web site…

Given a non-corrupt jar from Oracle, what method now works?


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

BTW, my suggestion #2.5 no longer works (boot classpath construction
changed
a few revisions back), but #1 and #2 should still get around the
problem.

-Bill

For completeness, I also tried specifying the class loader with
ojdbc14.jar
in the classpath as follows:

C:\ruby>set C
CLASSPATH=c:\ruby\ojdbc14.jar
C:\ruby>dir %CLASSPATH%
17/04/2009 03:14 PM 1,536,979 ojdbc14.jar
C:\ruby>jirb
irb(main):001:0> require ‘java’
=> false
irb(main):002:0> c =
java.lang.Class.for_name(“oracle.jdbc.driver.OracleDriver”, false,
JRuby.runtime.jruby_class_loader
)
=> #>
irb(main):003:0> con =
java.sql.DriverManager.getConnection(“jdbc:oracle:thin:@myhost:1521:mydb”,
“myuser”, “mypass”)
NativeException: java.sql.SQLException: No suitable driver

irb(main):004:0> puts java.sql.DriverManager.drivers.to_a
=> nil

Michael C.-4 wrote:

Given a non-corrupt jar from Oracle, what method now works?

I have success using plain JDBC and ActiveRecord using scripts as above.

I have success when the ojbc14.jar file is in my jruby/lib directory.

I have failure if I take ojdbc14.jar out of the lib directory, place is
somewhere else and set it in the shell CLASSPATH variable (which seems
weird
because I always thought the lib directory thing was just for convience
and
essentially the same as adding the JAR in CLASSPATH).

Rob


View this message in context:
http://www.nabble.com/Having-problems-accessing-Oracle-tp23070394p23128918.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email