4

我正在使用 JRuby 1.7.2 和 DataMapper,但我遇到了一个我找不到答案的错误。

我只是在测试 DataMapper 和 MySQL 5.5,看看当我围绕它构建时它是否能正常运行。这是我正在测试的文件:

require "data_mapper"
require "keys"

DataMapper.setup(:default, "mysql://#{$user}:#{$pass}@localhost/test_db")

当我运行它时,我得到了错误:

NameError: cannot load Java class com.mysql.jdbc.Driver

它指向 DataMapper.setup 行。

我的 Gemfile 应该没问题:

source :rubygems

gem "sinatra"
gem "trinidad"
gem "data_mapper"

# do a `sudo apt-get install libmysqlclient-dev` first
gem "dm-mysql-adapter"
gem "jdbc-mysql"

有什么我想念的吗?我已经在本地使用用户/密码设置了 MySQL。

4

4 回答 4

7

这是运行JDBC客户端时的常见错误MySQL。你需要确保你mysql-connector-java-bin.jar在类路径上。你可以从这里下载。

于 2013-01-10T18:21:24.680 回答
7

I'm using Rails 3.2.9 and to solve the problem I added this to my application.rb

if defined? JRUBY_VERSION
    require 'jdbc/mysql'
    Jdbc::MySQL.load_driver
end
于 2013-02-02T06:31:04.607 回答
2

actually, the correct answer is to work-around an incompatibility between 5.1.13 and 5.1.22 !

jdbc-mysql gem already contains the mysql-connector.jar it just ain't auto-loading anymore :

https://github.com/jruby/activerecord-jdbc-adapter/tree/master/jdbc-mysql (read the README)

you can also force the driver to auto-load using a Java system property e.g. from a cmd line :

jruby -J-Darjdbc.mysql.autoload=true -S rake ...

于 2013-01-17T08:31:41.040 回答
0

I had the same error when trying to use a mysql database from jruby/rails4. In my case this line was missing from ./Gemfile:

gem 'jdbc-mysql'
于 2014-09-03T21:16:38.943 回答