因此我可以将此 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")
最佳答案
The docs提出一些类似的建议:
# connection setup
ActiveRecord::Base.establish_connection(
:adapter => 'jdbc',
:driver => 'org.postgresql.Driver',
:url => 'jdbc:postgresql:sample_db;create=true'
)
或者
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'db/my-database'
)
这是假设您正在使用 JDBC 和 appropriate configuration .为此,我建议看看 Postgres' documentation .
那里提供的示例是用 Java 编写的:
String url = "jdbc:postgresql://localhost/test";
Properties props = new Properties();
props.setProperty("user","fred");
props.setProperty("password","secret");
props.setProperty("ssl","true");
Connection conn = DriverManager.getConnection(url, props);
String url = "jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true";
Connection conn = DriverManager.getConnection(url);
...但我会按照这些思路滚动:
ActiveRecord::Base.establish_connection(
:adapter => 'jdbc',
:driver => 'org.postgresql.Driver',
:url => 'jdbc:postgresql:sample_db;create=true'
:user => 'myuser'
:password => 's3cret!'
)
或者,有 JRuby-PG
问候,
关于ruby-on-rails - JRuby/基本 PostgreSQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777992/