문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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.

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top