Forum: JRuby Accessing PostgreSQL database using JDBC driver

C704ed8d173aeaf8ebc6d1c621fc9196?d=identicon&s=25 San Ksh (sanksh)
on 2014-01-26 04:59
Hi,
I am trying to access PostgreSQL database using jdbc-postgres driver. I
am using this code:

require 'java'
require 'jdbc/postgres'
Jdbc::Postgres.load_driver

String url = "jdbc:postgresql://localhost/postgres"
conn= DriverManager.getConnection(url,'postgres','postgres');
res=conn.query('select * from hoom.test')

The error I am getting is:
NameError: uninitialized constant DriverManager
  const_missing at org/jruby/RubyModule.java:2690
         (root) at db.rb:6

Thanks in advance for help
Caa2df9372ffa0a9e95b2bab1e8fea34?d=identicon&s=25 Karol Bucek (Guest)
on 2014-01-26 13:40
(Received via mailing list)
use *Java::JavaSql::DriverManager* or *java.sql.DriverManager*
C704ed8d173aeaf8ebc6d1c621fc9196?d=identicon&s=25 San Ksh (sanksh)
on 2014-01-27 09:51
Hi,
Thanks for help. I am able to get connection but unable to read the
records. I am using following code:

require 'rubygems'
require 'java'
require 'jdbc/postgres'
Jdbc::Postgres.load_driver

url = "jdbc:postgresql://localhost/postgres"
conn= *java::sql::DriverManager.getConnection(url,'postgres','postgres')
st=conn.create_statement
query="select * from test"
rs = st.execute_query(query)
rs.each {|row| puts row }

The error I am getting is:
NoMethodError: undefined method `create_statement' for
[#<Java::OrgPostgresqlJdb
c4::Jdbc4Connection:0x11e04129>]:Array
  (root) at db.rb:8
The code example is from this link:

https://github.com/jruby/jruby/wiki/JDBC
Caa2df9372ffa0a9e95b2bab1e8fea34?d=identicon&s=25 Karol Bucek (Guest)
on 2014-01-27 10:28
(Received via mailing list)
the error message simply says it all conn is an Array due the * you
probably wanted :

conn= java::sql::DriverManager.getConnection(url,'postgres','postgres')

I recommend you learn some 'more' Ruby first cause otherwise it's going
to
take some.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.