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

发布于 2024-09-26 14:42:02 字数 1079 浏览 5 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独难免 2024-10-03 14:42:02

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

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

This will import the JDBC driver JAR file for MySQL。

你加载驱动了吗?如果您使用此 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
      }
    }
  }
}

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

In the SBT project class there should be a line:

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

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:

// 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

梅倚清风 2024-10-03 14:42:02

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

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

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

In the project/plugins.sbt file add a line

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

Then if your in the sbt shell, restart it.

您的好友蓝忘机已上羡 2024-10-03 14:42:02

必须在 build.sbt 中配置 MySQL 依赖项。目前的风格是声明库依赖关系,如下所示:

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

Seq 中添加以下内容以添加 mysql:

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

注意,+ 表示它将获取最新的次要版本; 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:

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 Seq to add 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文