How to connect to Oracle using JRuby & JDBC

后端 未结 4 748
鱼传尺愫
鱼传尺愫 2020-12-29 01:00

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
         


        
相关标签:
4条回答
  • 2020-12-29 01:21

    Have you got the Oracle client installed? you probably need at least the jdbc driver files from the client

    0 讨论(0)
  • 2020-12-29 01:24

    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
    
    
    0 讨论(0)
  • 2020-12-29 01:36

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

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

    0 讨论(0)
  • 2020-12-29 01:40
    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 讨论(0)
提交回复
热议问题