【问题标题】:How to use MySQL JDBC driver in an SBT Scala project?如何在 SBT Scala 项目中使用 MySQL JDBC 驱动程序?
【发布时间】:2011-04-23 04:59:48
【问题描述】:

当我在 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

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

【问题讨论】:

    标签: mysql scala jdbc sbt


    【解决方案1】:

    必须在 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(撰写本文时的当前版本)。

    【讨论】:

      【解决方案2】:

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

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

      如果你在 sbt shell 中,重启它。

      【讨论】:

        【解决方案3】:

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

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

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

        您是否加载了驱动程序?如果你使用这个 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

        【讨论】:

        • 遇到了同样的问题,这个解决了!
        猜你喜欢
        • 2016-09-24
        • 1970-01-01
        • 2012-11-20
        • 2017-08-27
        • 2013-03-22
        • 2015-12-13
        • 2016-02-29
        • 2012-07-20
        • 2011-09-10
        相关资源
        最近更新 更多