如何在 SBT Scala 项目中使用 MySQL JDBC 驱动程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3896153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:23:59  来源:igfitidea点击:

How to use MySQL JDBC driver in an SBT Scala project?

mysqlscalajdbcsbt

提问by Ivan

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:

当我在 SBT 会话期间第一次运行我的项目时,它在尝试访问 MySQL 数据库时抛出以下异常:

java.lang.NoClassDefFoundError: scala/Ordered

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:

当我再次运行它时(以及它之后的任何时间,在同一个 SBT 会话期间),它会抛出一个不同的:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/...

java.sql.SQLException: 找不到适合 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.

当我使用 NetBeans 时,相同的代码运行正常。现在,当我使用 SBT 进行构建并使用 Kate 手动编辑和管理我的项目时,我收到了这些运行时错误。

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.

MySQL JDBC 驱动程序(直接从 MySQL.com 下载)JAR 位于项目的 lib 目录中,我放在那里的所有其他库都可以正常工作。

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 questionthat looks pretty related, but still no answer.

我看到一个看起来很相关的问题,但仍然没有答案。

采纳答案by olle kullberg

In the SBT project class there should be a line:

在 SBT 项目类中应该有一行:

 // Declare MySQL connector Dependency
  val mysql = "mysql" % "mysql-connector-java" % "5.1.12"

This will import the JDBC driver JAR file for MySQL.

这将为 MySQL 导入 JDBC 驱动程序 JAR 文件。

Did you load the driver? If you use this Util class to fetch the connections, the driver will be loaded exactly one time:

你加载驱动了吗?如果你使用这个 Util 类来获取连接,驱动程序将被加载一次:

// Util Class
object DaoUtil {
  import java.sql.{DriverManager, Connection}

  private var driverLoaded = false

  private def loadDriver()  {
    try{
      Class.forName("com.mysql.jdbc.Driver").newInstance
      driverLoaded = true
    }catch{
      case e: Exception  => {
        println("ERROR: Driver not available: " + e.getMessage)
        throw e
      }
    }
  }

  def getConnection(dbc: DbConnection): Connection =  {
    // Only load driver first time
    this.synchronized {
      if(! driverLoaded) loadDriver()
    }

    // Get the connection
    try{
      DriverManager.getConnection(dbc.getConnectionString)
    }catch{
      case e: Exception  => {
        println("ERROR: No connection: " + e.getMessage)
        throw e
      }
    }
  }
}

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

代码取自我前段时间写的一个简单的 SBT - MySQL 教程。如果你想下载完整的教程,请参见http://github.com/ollekullberg/SimpleOrder

回答by samthebest

In the project/plugins.sbt file add a line

在 project/plugins.sbt 文件中添加一行

libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.12"

Then if your in the sbt shell, restart it.

然后,如果您在 sbt shell 中,请重新启动它。

回答by Nathaniel Ford

The MySQL dependency must be configured in your build.sbt. Currently the style is to declare library dependencies like so:

MySQL 依赖项必须在您的build.sbt. 目前的风格是像这样声明库依赖项:

libraryDependencies ++= {
  val liftVersion = "2.5.1"
  Seq(
    "net.liftweb"       %% "lift-webkit"        % liftVersion        % "compile",
    "net.liftweb"       %% "lift-mapper"        % liftVersion        % "compile",
    //etc
  )
}

Add the following inside the Seqto add mysql:

在里面添加以下内容Seq来添加mysql:

"mysql" % "mysql-connector-java" % "5.1.+"

Note that the +means that it will get the latest minor version; anything above 5.1, such as 5.1.27(the current version at time of writing).

请注意,这+意味着它将获得最新的次要版本;以上任何内容5.1,例如5.1.27(撰写本文时的当前版本)。