我想连接到db并使用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
任何人都可以帮我解决这个问题吗?谢谢!
答案 0 :(得分:1)
看起来你在这一行有问题:
select_stmt = #@conn.create_statement
#字符已注释掉该分配,因此您的select_stmt变量未初始化。
答案 1 :(得分:0)
select_stmt = #@conn.create_statement
这是对右边的评论。所以在下一行:
reselt_set = select_stmt.execute_query(select_sql)
select_stmt为nil ...因此出现错误消息。