0

I want to connect to the db and execute a very simple query using JRuby. It is establishing the connection but failing to execute the query. Below is the code:

  require 'oracle_connection'

 #Edit these for your database schema

class OracleConnectionDriver

   def initialize(user, pwd, url)
      print "Run at #{Time.now} using JRuby #{RUBY_VERSION}\n\n"user
      @conn = OracleConnection.new(user, pwd, url)
      puts #@conn, "\n"
   end

  def is_user?(user_name)
    puts "Checking if user exists: "
      select_sql = <<-EOF
        SELECT COUNT(*)
        FROM users
        WHERE username = upper('#{user_name})
        EOF

    select_stmt = #@conn.create_statement
    reselt_set = select_stmt.execute_query(select_sql)
     while(result_set.next)
        puts " Resultset [#{result_set.getInt(1)}]"
     end
   end


  print "\nEnded at #{Time.now}\n"

end

test_connection =  OracleConnectionDriver.new('USERNAME', 'PWD', 'jdbc:oracle:thin:@XXXXX:8888/YYYY')
test_connection.is_user?('ABC')

While running this it is failing with the following error message:

ruby test_connection.rb

Ended at 2013-09-16 14:28:31 -0700
Run at 2013-09-16 14:28:31 -0700 using JRuby 1.9.2


Checking if user exists:
NoMethodError: undefined method `execute_query' for nil:NilClass
is_user_p_ at test_connection.rb:27
  (root) at test_connection.rb:40

Can anyone help me in fixing this? Thanks!

itsh
  • 1,063
  • 3
  • 14
  • 28

2 Answers2

1

Looks like you have a problem on this line:

select_stmt = #@conn.create_statement

The # character has commented out the assignment so your select_stmt variable is not initialized.

Krishnan
  • 11
  • 1
0
select_stmt = #@conn.create_statement

That's a comment on the right. So on the next line:

reselt_set = select_stmt.execute_query(select_sql)

select_stmt is nil...hence the error message.

7stud
  • 46,922
  • 14
  • 101
  • 127