2009-04-16 13 views
6

Primo approccio: bare metalCome connettersi a Oracle tramite JRuby e 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 

Il risultato di quanto sopra è:

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

Secondo approccio: Attivo 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") 

Il risultato di questo è:

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) 

Essenzialmente lo stesso errore, non importa come ci vado.

sto usando JRuby 1.2.0 e ho ojdbc14.jar nella mia cartella lib JRuby

gemme:

  • ActiveRecord-JDBC (0,5)
  • activerecord-jdbc-adattatore (0.9.1)
  • ActiveRecord (2.2.2)

che cosa mi manca?

Grazie,

+0

Sarò curioso di vedere se così può battere il forum rubino (http://www.ruby-forum.com/topic/ 184414) –

+0

Originariamente pubblicato su Nabble http://www.nabble.com/Having-problems-accessing-Oracle-td23070394.html Immagino che siano stati propinati a Ruby Forum in qualche modo ... – Rob

risposta

5

Si scopre che il mio file ojdbc14.jar era danneggiato.

Inoltre, il file jar DEVE essere nella directory jruby/lib. Semplicemente averlo sul classpath non funziona.

+0

appare in questi giorni averlo nella variabile di ambiente (specifica di jruby) CLASSPATH è anche abbastanza, ma semplicemente facendo una richiesta 'ojdbc14.jar' non lo è. – rogerdpack

+0

sembra che in questi giorni sia sufficiente richiedere il jar, non deve più essere nella directory lib (1.6.0RC2) – rogerdpack

0

Hai installato il client Oracle? probabilmente avete bisogno almeno dei file del driver jdbc dal client

+0

Sì, ma non pensavo sarebbe richiesto con questo tipo di connessione "sottile". – Rob

+0

sì, il barattolo è in genere sufficiente. – rogerdpack

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 
+0

Sto usando il tuo esempio ora, ma quello che non capisco è come avrebbe eseguito i comandi. Puoi per favore tornare da me su questo? – user3505901

3

e poi di usarlo dopo la creazione:

 
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 

Problemi correlati