JRuby + Sqlitejdbc:找不到适用于jdbc:sqlite的驱动程序: [英] JRuby + Sqlitejdbc : No suitable driver found for jdbc:sqlite:
问题描述
在只有用户帐户的计算机上,我想将作业的输出转储到sqlite数据库而不是文本文件中.为此,我从jarfile运行jruby.
On a machine I have only a user account on, I want to dump the output of a job into an sqlite database instead of textfiles. To this end, I run jruby from the jarfile.
使用本地GEM_HOME
中的gems(dbi, dbd/Jdbc, jdbc/sqlite3
)的方法不起作用(未找到合适的驱动程序),并且还会从gems中产生弃用消息("include_class已弃用.请使用java_import.")
The approach usings gems (dbi, dbd/Jdbc, jdbc/sqlite3
) from a local GEM_HOME
did not work (No suitable driver found) and also produces deprecation messages from the gems ("include_class is deprecated. Use java_import.")
我转向Zentus的sqlitejdbc-v056.jar并在路径中运行了带有Zentus的JRuby:
I turned to Zentus' sqlitejdbc-v056.jar and ran JRuby with Zentus' in the path:
java -cp .:sqlitejdbc-v056.jar -jar jruby-complete-1.7.0.preview1.jar test.rb
其中的test.rb受到 http://www.zentus.com/sqlitejdbc/和如何在JRuby中初始化SQLite3 JDBC驱动程序?:
where test.rb in inspired by http://www.zentus.com/sqlitejdbc/ and How to initialize the SQLite3 JDBC driver in JRuby? :
require 'java'
require '/home/jens/jruby/sqlitejdbc-v056.jar'
org.sqlite.JDBC # load the driver so DriverManager detects it
p clazz = Java::JavaClass.for_name("org.sqlite.JDBC")
java.sql.DriverManager.registerDriver( clazz )
#Java::OrgSqlite::JDBC # alternate means of same
puts "enumerating..."
java.sql.DriverManager.getDrivers.each{ |e| puts e }
connection = java.sql.DriverManager.getConnection 'jdbc:sqlite:/home/jens/jruby/test.db'
begin
statement = connection.createStatement
...
ensure
connection.close
end
我从中得到的输出是:
class org.sqlite.JDBC
enumerating...
sun.jdbc.odbc.JdbcOdbcDriver@73415727
DriverManager.java:602:in `getConnection': java.sql.SQLException: No suitable driver found for jdbc:sqlite:/home/jens/jruby/test.db
from DriverManager.java:207:in `getConnection'
from NativeMethodAccessorImpl.java:-2:in `invoke0'
...
奇怪的是,DriverManager列出了该驱动程序,但认为该驱动程序不适合sqlite.
Curiously, the driver is listed by the DriverManager, but not deemed suitable for sqlite.
我期待任何建议.
推荐答案
首先,您应该使用捆绑程序来管理gem依赖项. Bundler使用Gemfile列出所需的宝石(将其放在脚本所在的位置).并确保jruby在您的路径中.
First of all you should use bundler to manage your gem dependencies. Bundler uses a Gemfile to list the gems you need (place it where your script is). And make sure that jruby is in your path.
您的情况下,Gemfile应该包含:
In your case the Gemfile should contain:
source 'http://rubygems.org'
gem "activerecord-jdbcsqlite3-adapter", ">= 1.2"
然后执行:
bundle install --path vendor/bundle
然后按如下所示修改脚本:
Then modify your script like this:
require 'rubygems'
require "java"
require 'bundler/setup'
require 'jdbc/sqlite3'
Bundler.require
org.sqlite.JDBC # load the driver so DriverManager detects it
p clazz = Java::JavaClass.for_name("org.sqlite.JDBC")
java.sql.DriverManager.registerDriver( clazz )
puts "enumerating..."
java.sql.DriverManager.getDrivers.each{ |e| puts e }
connection = java.sql.DriverManager.getConnection 'jdbc:sqlite:/home/jens/jruby/test.db'
begin
statement = connection.createStatement
ensure
connection.close
end
这篇关于JRuby + Sqlitejdbc:找不到适用于jdbc:sqlite的驱动程序:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!