Accessing PostgreSQL database using JDBC driver

I am trying to access PostgreSQL database using jdbc-postgres driver. I
am using this code:

require ‘java’
require ‘jdbc/postgres’

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/
(root) at db.rb:6

Thanks in advance for help

use Java::JavaSql::DriverManager or java.sql.DriverManager

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’

url = “jdbc:postgresql://localhost/postgres”
conn= *java::sql::DriverManager.getConnection(url,‘postgres’,‘postgres’)
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
(root) at db.rb:8
The code example is from this link:

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
take some.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs