因此,我可以使用此gem进行活动记录:https : //github.com/jruby/activerecord-jdbc-adapter
但是,我想在JRuby上使用以下代码连接到旧版数据库并运行一些手动查询,如何使用JRuby / Rails 4.0.0?
require 'pg'
conn = PGconn.connect("192.168.0.2", 5432, '', '', "mydb", "postgres", "password")
因此,这里的示例起作用了。
https://github.com/jruby/jruby/wiki/JDBC
def vegetableFinder(vegetable)
# This function takes a hashmap of vegetables and attempts to
# find them from our grocery database. For each item found, we
# call our 'makevegetablesoup' function.
# Load all required gems
require "rubygems"
require "jdbc/mysql"
require "java"
begin
# Prep the connection
Java::com.mysql.jdbc.Driver
userurl = "jdbc:mysql://HOST/DATABASE"
connSelect = java.sql.DriverManager.get_connection(userurl, "USERNAME", "PASSWORD")
stmtSelect = connSelect.create_statement
# Define the query
selectquery = %q{SELECT name, type, size, price
FROM vegetables
WHERE type = "#{vegetable["type"]}"
AND size = "#{vegetable["size"]}}
# Execute the query
rsS = stmtSelect.execute_query(selectquery)
# For each row returned do some stuff
while (rsS.next) do
veg = Hash.new
veg["vegname"] = rsS.getObject("name")
veg["vegtype"] = rsS.getObject("type")
veg["vegprice"] = rsS.getObject("size")
veg["vegsize"] = rsS.getObject("price")
makevegetablesoup(veg)
end
end
# Close off the connection
stmtSelect.close
connSelect.close
return truth
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句