如何使用 JRuby 和 JDBC 连接到 Oracle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/755207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to Oracle using JRuby & JDBC
提问by Robert Brown
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
第二种方法: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.
无论我如何处理,本质上都是相同的错误。
I'm using JRuby 1.2.0 and I have ojdbc14.jar in my JRuby lib directory
我使用的是 JRuby 1.2.0,我的 JRuby lib 目录中有 ojdbc14.jar
Gems:
宝石:
- ActiveRecord-JDBC (0.5)
- activerecord-jdbc-adapter (0.9.1)
- activerecord (2.2.2)
- ActiveRecord-JDBC (0.5)
- activerecord-jdbc-adapter (0.9.1)
- 活动记录 (2.2.2)
What am I missing?
我错过了什么?
Thanks,
谢谢,
采纳答案by Robert Brown
It turns out that my ojdbc14.jar file was corrupt.
原来我的 ojdbc14.jar 文件已损坏。
Further, the jar file MUST be in the jruby/lib directory. Simply having it on the classpath does not work.
此外,jar 文件必须在 jruby/lib 目录中。简单地将它放在类路径上是行不通的。
回答by Bryan Castillo
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
回答by rogerdpack
and then to use it after creation:
然后在创建后使用它:
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
回答by Colin Pickard
Have you got the Oracle client installed? you probably need at least the jdbc driver files from the client
你有没有安装Oracle客户端?您可能至少需要来自客户端的 jdbc 驱动程序文件