如何在 SBT Scala 项目中使用 MySQL JDBC 驱动程序?
当我在 SBT 会话期间第一次运行我的项目时,它在尝试访问 MySQL 数据库时抛出以下异常:
java.lang.NoClassDefFoundError:scala/Ordered
当我再次运行它时(以及之后的任何时间,在同一个 SBT 会话期间),它会抛出一个不同的错误:
java.sql.SQLException:没有找到适合 jdbc 的驱动程序:mysql://localhost/...
当我使用 NetBeans 时,相同的代码工作正常。现在,当我使用 SBT 进行构建并使用 Kate 手动编辑和管理我的项目时,我收到了这些运行时错误。
MySQL JDBC 驱动程序(直接从 MySQL.com 下载)JAR 位于项目的 lib 目录中,我放在那里的所有其他库都可以正常工作。
这是代码:
import java.sql._
...
// read
val dbc : Connection = DriverManager.getConnection("jdbc:mysql://localhost/...")
val st : Statement = dbc.createStatement
val rs : ResultSet = st.executeQuery("SELECT ...")
if(rs.first) result = rs.getDouble("field")
dbc.close
...
// write
val dbc : Connection = DriverManager.getConnection("jdbc:mysql://localhost/...")
val st : Statement = dbc.createStatement
st.execute("UPDATE ...")
dbc.close
我看到了一个问题 看起来很相关,但仍然没有答案。
When I run my project for the first time during an SBT session, it throws the following exception when trying to access to a MySQL database:
java.lang.NoClassDefFoundError: scala/Ordered
When I run it again (and any time after it, during the same SBT session), it throws a different one:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/...
When I was using NetBeans, the same code was working Ok. Now, As I use SBT for building and Kate to edit and manage my project manually, I get these runtime errors.
MySQL JDBC driver (downloaded right from MySQL.com) JAR is in project's lib directory and all the other libraries I've put there work ok.
Here is the code:
import java.sql._
...
// read
val dbc : Connection = DriverManager.getConnection("jdbc:mysql://localhost/...")
val st : Statement = dbc.createStatement
val rs : ResultSet = st.executeQuery("SELECT ...")
if(rs.first) result = rs.getDouble("field")
dbc.close
...
// write
val dbc : Connection = DriverManager.getConnection("jdbc:mysql://localhost/...")
val st : Statement = dbc.createStatement
st.execute("UPDATE ...")
dbc.close
I've seen a question that looks pretty related, but still no answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 SBT 项目类中应该有一行:
This will import the JDBC driver JAR file for MySQL。
你加载驱动了吗?如果您使用此 Util 类来获取连接,则驱动程序将被加载一次:
该代码取自我前段时间编写的一个简单的 SBT - MySQL 教程。如果您想下载完整的教程,请参阅 http://github.com/ollekullberg/SimpleOrder
In the SBT project class there should be a line:
This will import the JDBC driver JAR file for MySQL.
Did you load the driver? If you use this Util class to fetch the connections, the driver will be loaded exactly one time:
The code is taken from a simple SBT - MySQL tutorial I wrote some time ago. If you want to download the complete tutorial, see http://github.com/ollekullberg/SimpleOrder
在 project/plugins.sbt 文件中添加一行
然后,如果您在 sbt shell 中,请重新启动它。
In the project/plugins.sbt file add a line
Then if your in the sbt shell, restart it.
必须在
build.sbt
中配置 MySQL 依赖项。目前的风格是声明库依赖关系,如下所示:在
Seq
中添加以下内容以添加 mysql:注意,
+
表示它将获取最新的次要版本;5.1
以上的任何内容,例如5.1.27
(撰写本文时的当前版本)。The MySQL dependency must be configured in your
build.sbt
. Currently the style is to declare library dependencies like so:Add the following inside the
Seq
to add mysql:Note that the
+
means that it will get the latest minor version; anything above5.1
, such as5.1.27
(the current version at time of writing).