如何使用JRuby&连接到Oracle JDBC [英] How to connect to Oracle using JRuby & JDBC

查看:84
本文介绍了如何使用JRuby&连接到Oracle JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一种方法:裸机

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

以上结果为:

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

第二种方法:活动记录

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")

其结果是:

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.

我正在使用JRuby 1.2.0,并且在我的JRuby lib目录中有ojdbc14.jar

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

宝石:

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

我想念什么?

谢谢

推荐答案

原来,我的ojdbc14.jar文件已损坏.

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

此外,jar文件必须位于jruby/lib目录中.只是将其放在类路径上是行不通的.

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

这篇关于如何使用JRuby&连接到Oracle JDBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆