1

So I can use this gem for active record: https://github.com/jruby/activerecord-jdbc-adapter

But, I want to use the following code on JRuby to connect to a legacy db and run some manual queries, how do I do using JRuby / Rails 4.0.0?

require 'pg'
conn = PGconn.connect("192.168.0.2", 5432, '', '', "mydb", "postgres", "password")

2 Answers 2

1

The docs suggest something along the lines of:

# connection setup
ActiveRecord::Base.establish_connection(
  :adapter => 'jdbc',
  :driver => 'org.postgresql.Driver',
  :url => 'jdbc:postgresql:sample_db;create=true'
)

Or

ActiveRecord::Base.establish_connection(
  adapter: 'postgresql',
  database: 'db/my-database'
)

This is assuming you're using JDBC and the appropriate configuration. For this, I suggest taking a look at Postgres' documentation.

Sample provided over there is written in 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);

...but I'd roll with something along those lines:

ActiveRecord::Base.establish_connection(
  :adapter => 'jdbc',
  :driver => 'org.postgresql.Driver',
  :url => 'jdbc:postgresql:sample_db;create=true'
  :user => 'myuser'
  :password => 's3cret!'
)

Alternatively, there's JRuby-PG

Regards,

Sign up to request clarification or add additional context in comments.

1 Comment

Having some problems getting any of this to work.. Will keep trying
0

So the example here worked..

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.