第一种方法:裸机
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)
无论我怎么做,基本上都是同样的错误。
我正在使用JRuby 1.2.0,我的JRuby lib目录中有ojdbc14.jar
宝石:
我错过了什么?
谢谢,
答案 0 :(得分:5)
事实证明我的ojdbc14.jar文件已损坏。
此外,jar文件必须位于jruby / lib目录中。简单地在类路径上使用它是行不通的。
答案 1 :(得分:5)
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
答案 2 :(得分:3)
然后在创建后使用它:
b = con.create_statement rs=b.execute_query(“select BANNER from SYS.V_$VERSION”) while(rs.next()) p rs.getObject(1) # get first column end rs.close and how to deal with oracle timestamps (if column 3 is a timestamp, for example): >> rs.getObject(3).timestamp_value.to_string => “1970-01-01 00:00:01.0″ >> Date.parse(rs.getObject(3).timestamp_value.to_string) # or you can use time, like >> as_ruby_time= Date.parse(rs.getObject(3).timestamp_value.to_string).to_time # 1.9 has this method # or >> as_ruby_time = Time.at(0) + rs.getObject(3).timestamp_value.get_time
答案 3 :(得分:0)
您是否安装了Oracle客户端?您可能至少需要来自客户端的jdbc驱动程序文件