我想连接到数据库并使用 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
谁能帮我解决这个问题?谢谢!