我怎样才能在jruby中使用java.util.properties?

问题描述 投票:0回答:1

我正在使用本网站提供的课程:https://www.oracle.com/technetwork/articles/dsl/jruby-oracle11g-330825.html

我正在设置SSL,我需要将java.util.properties列表传递给Driver manager。

我想我会做这样的事......

# jdbc_connection.rb

require 'java'

java_import 'oracle.jdbc.OracleDriver'
java_import 'java.sql.DriverManager'
java_import 'java.util.Properties' #<---Import here

class OracleConnection

  @conn = nil

  def initialize (url)
    @url = url
    #I want to create the array of properties here and populate it with my SSL properties. I'm really lost here.

    # Load driver class
    oradriver = OracleDriver.new

    DriverManager.registerDriver oradriver
    #I want to pass the Properties to DriverManager.
    @conn = DriverManager.get_connection url, properties 
    @conn.auto_commit = false

  end

我挂了如何在ruby中创建属性并传递它们。有任何想法吗?

java ruby-on-rails ruby jruby
1个回答
1
投票

java.util.Properties Java类视为Ruby类(它的所有API方法都可用)

如果你看看文档Properties extends HashtableHashtable是一个(实现)Map

JRuby为所有地图类型提供了扩展,使其非常像Ruby Hash

properties = java.util.Properties.new
properties.put 'a.ssl.key', 'A-VALUE' # Java style (put inherited from Hashtable)
properties['another.ssl.key'] = 'ANOTHER' # Ruby Hash style
© www.soinside.com 2019 - 2024. All rights reserved.