# Having problems accessing Oracle

**URL:** <https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654>\
**Category:** JRuby\
**Created:** [April 16, 2009, 4:28am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654 "2009-04-16T04:28:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 16, 2009, 4:28am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/1 "2009-04-16T04:28:21Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 16, 2009, 10:50am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/2 "2009-04-16T10:50:08Z")

</div>

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](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 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,

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 16, 2009, 8:53pm UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/3 "2009-04-16T20:53:49Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://Nabble.com).

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 17, 2009, 3:03am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/4 "2009-04-17T03:03:25Z")

</div>

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.:

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

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 17, 2009, 2:34am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/5 "2009-04-17T02:34:09Z")

</div>

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](http://www.nabble.com/Having-problems-accessing-Oracle-tp23070394p23088562.html)  
Sent from the JRuby - User mailing list archive at [Nabble.com](http://Nabble.com).

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 17, 2009, 3:44am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/6 "2009-04-17T03:44:31Z")

</div>

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](http://www.nabble.com/Having-problems-accessing-Oracle-tp23070394p23089710.html)  
Sent from the JRuby - User mailing list archive at [Nabble.com](http://Nabble.com).

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 17, 2009, 11:19am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/7 "2009-04-17T11:19:24Z")

</div>

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](http://www.nabble.com/Having-problems-accessing-Oracle-tp23070394p23094079.html)  
Sent from the JRuby - User mailing list archive at [Nabble.com](http://Nabble.com).

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 18, 2009, 1:29am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/8 "2009-04-18T01:29:30Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 17, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/9 "2009-04-17T19:25:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 20, 2009, 3:28am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/10 "2009-04-20T03:28:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rjberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@rjberry](https://www.ruby-forum.com/u/rjberry)\
**Post date:** [April 20, 2009, 2:48am UTC](https://www.ruby-forum.com/t/having-problems-accessing-oracle/165654/11 "2009-04-20T02:48:41Z")

</div>

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](http://www.nabble.com/Having-problems-accessing-Oracle-tp23070394p23128918.html)  
Sent from the JRuby - User mailing list archive at [Nabble.com](http://Nabble.com).

* * *

To unsubscribe from this list, please visit:

```
http://xircles.codehaus.org/manage_email

```
