gpt4 book ai didi

java - 如何在 Ruby 程序中捕获 java.sql.sqlException?

转载 作者:行者123 更新时间:2023-11-30 04:37:48 25 4
gpt4 key购买 nike

我确信我在这里做了一些愚蠢的事情。我正在使用 Ruby 1.9.2-p312、jruby 1.6.8,并将 --1.9 选项设置为默认值。这是我第一次尝试拯救 SQLException。我故意通过拔掉以太网连接来使此代码失败。

require 'java'
require 'lib/jt400-6.1'
require 'date'
require 'bigdecimal'

java_import 'com.ibm.as400.access.AS400JDBCDriver'def initialize(parms={})

class DB
def initialize(parms = {})
begin
@connection ||= java.sql.DriverManager.get_connection "jdbc:as400://System/",parms[:user], parms[:pass]
rescue java.sql.SQLException
puts "Error connecting to iSeries. Error code: #{java.sql.SQLException.getErrorCode()}
SQL State: #{java.sql.SQLException.getSQLState()}."
end
end
end

这会产生以下错误:

NoMethodError - undefined method `getErrorCode' for Java::JavaSql::SQLException:Class:
/home/nick/Projects/OrderBridge/app/db.rb:17:in `initialize'
/home/nick/Projects/OrderBridge/app/order-processor.rb:208:in `prepare'
/home/nick/Projects/OrderBridge/app/orderbridge.rb:43:in `POST /advanced'
org/jruby/RubyMethod.java:117:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:1212:in `compile!'
org/jruby/RubyProc.java:258:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `route!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:801:in `route_eval'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:785:in `route!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:822:in `process_route'
org/jruby/RubyKernel.java:1226:in `catch'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:820:in `process_route'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:784:in `route!'
org/jruby/RubyArray.java:1620:in `each'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:783:in `route!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:886:in `dispatch!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:719:in `call!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `invoke'
org/jruby/RubyKernel.java:1226:in `catch'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:871:in `invoke'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:719:in `call!'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/base.rb:705:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/methodoverride.rb:21:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/path_traversal.rb:16:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/json_csrf.rb:17:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/base.rb:47:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb:22:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:205:in `context'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/session/abstract/id.rb:200:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/nulllogger.rb:9:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/head.rb:9:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/sinatra-1.3.2/lib/sinatra/showexceptions.rb:21:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/lint.rb:48:in `_call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/lint.rb:36:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/showexceptions.rb:24:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/commonlogger.rb:20:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/chunked.rb:43:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/content_length.rb:14:in `call'
/home/nick/.rvm/gems/jruby-1.6.8/gems/rack-1.4.1/lib/rack/handler/webrick.rb:59:in `service'
/home/nick/.rvm/rubies/jruby-1.6.8/lib/ruby/1.9/webrick/httpserver.rb:111:in `service'
/home/nick/.rvm/rubies/jruby-1.6.8/lib/ruby/1.9/webrick/httpserver.rb:70:in `run'
/home/nick/.rvm/rubies/jruby-1.6.8/lib/ruby/1.9/webrick/server.rb:183:in `start_thread'

最佳答案

您想将异常捕获到变量中。

class DB
def initialize(parms = {})
begin
@connection ||= java.sql.DriverManager.get_connection "jdbc:as400://System/",parms[:user], parms[:pass]
rescue java.sql.SQLException => e
puts "Error connecting to iSeries. Error code: #{e.getErrorCode()}
SQL State: #{e.getSQLState()}."
end
end
end

这样,您就不会在异常的类上调用 .getErrorCode(),而是在异常的实例上调用。

Dealing with exceptions in jRuby

关于java - 如何在 Ruby 程序中捕获 java.sql.sqlException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13032438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com