0

我想连接到数据库并使用 JRuby 执行一个非常简单的查询。它正在建立连接但未能执行查询。下面是代码:

  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')

运行此程序时失败并显示以下错误消息:

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

谁能帮我解决这个问题?谢谢!

4

2 回答 2

1

看起来你在这条线上有问题:

select_stmt = #@conn.create_statement

# 字符已注释掉分配,因此您的 select_stmt 变量未初始化。

于 2013-09-16T21:55:12.957 回答
0
select_stmt = #@conn.create_statement

这是右边的评论。所以在下一行:

reselt_set = select_stmt.execute_query(select_sql)

select_stmt 为零...因此出现错误消息。

于 2013-09-16T21:55:43.177 回答