Forum: JRuby Having problems accessing Oracle

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
89316fb47af779e33e9d36d08a5eddd6?d=identicon&s=25 Robert Brown (rob99)
on 2009-04-16 04:28
(Received via mailing list)
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,
94749e563af7dd7b26f866c1c34cb8f9?d=identicon&s=25 Adrian Mowat (amowat)
on 2009-04-16 10:50
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: http://www.ruby-forum.com/topic/184366#new

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 Brown 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,
2e793cdf2d72aecd526a081ae2f1865b?d=identicon&s=25 Johnny P (Guest)
on 2009-04-16 20:53
(Received via mailing list)
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 <rob99brown@yahoo.com> 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
89316fb47af779e33e9d36d08a5eddd6?d=identicon&s=25 Robert Brown (rob99)
on 2009-04-17 02:34
(Received via mailing list)
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:
<pre>
sqltest.rb:14: wrong # of arguments(3 for 1) (ArgumentError)
</pre>

It seems that JRuby cannot for the overload...

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


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
47ba65434738bc83d9f97bf2bd77bd31?d=identicon&s=25 Bill Dortch (Guest)
on 2009-04-17 03:03
(Received via mailing list)
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.

2. 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
89316fb47af779e33e9d36d08a5eddd6?d=identicon&s=25 Robert Brown (rob99)
on 2009-04-17 03:44
(Received via mailing list)
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 Dortch 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-Or...
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
89316fb47af779e33e9d36d08a5eddd6?d=identicon&s=25 Robert Brown (rob99)
on 2009-04-17 11:19
(Received via mailing list)
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-Or...
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
47ba65434738bc83d9f97bf2bd77bd31?d=identicon&s=25 Bill Dortch (Guest)
on 2009-04-17 19:25
(Received via mailing list)
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
76aed56df3e087c55e6961f9fca7a6aa?d=identicon&s=25 Michael Campbell (Guest)
on 2009-04-18 01:29
(Received via mailing list)
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
89316fb47af779e33e9d36d08a5eddd6?d=identicon&s=25 Robert Brown (rob99)
on 2009-04-20 02:48
(Received via mailing list)
Michael Campbell-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-Or...
Sent from the JRuby - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
89316fb47af779e33e9d36d08a5eddd6?d=identicon&s=25 Robert Brown (rob99)
on 2009-04-20 03:28
(Received via mailing list)
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
This topic is locked and can not be replied to.