Spray.io,slick,sqlite3:找不到合适的驱动程序

时间:2014-11-20 16:41:53

标签: scala sqlite slick

我是java / scala生态系统中的超级菜鸟,我无法让这个示例项目工作。

package com.example

import akka.actor.Actor
import spray.routing._
import spray.http._
import MediaTypes._
import spray.json._
import DefaultJsonProtocol._
import scala.slick.driver.SQLiteDriver.simple._

class MyServiceActor extends Actor with MyService {

  def actorRefFactory = context

  def receive = runRoute(myRoute)
}


trait MyService extends HttpService {
  val cars: TableQuery[Cars] = TableQuery[Cars]

  val db = Database.forURL("jdbc:sqlite:./test.sqlite3", driver = "scala.slick.driver.SQLiteDriver")

  val myRoute = path("") {
    get {
      respondWithMediaType(`application/json`) {
        complete {

          val result = db.withSession {

            implicit session =>

              cars.ddl.create
              val myCar = Car(-1, "Ford", "Taurus", 2015)
              cars.insert(myCar)
          }
          "Hi"
        }
      }
    }
  }
}

Here's the project。它直接基于spray.io的示例。

我收到spray java.sql.SQLException: No suitable driver found for jdbc:sqlite:./test.sqlite3错误。显然我没有正确加载驱动程序,但我已经在互联网上搜索了正确的语法,并且很难找到任何东西。我错过了什么?

谢谢!

P.S。 Scala 2.11.2,Spray 1.3.2,Slick 2.1.0。

1 个答案:

答案 0 :(得分:2)

  1. 您需要将sqlite jdbc驱动程序添加到依赖项中:
  2. 将build.sbt更新为:

    libraryDependencies ++= {
      val akkaV = "2.3.6"
      val sprayV = "1.3.2"
      Seq(
        "com.typesafe.akka"   %%  "akka-actor"    % akkaV,
        "com.typesafe.akka"   %%  "akka-testkit"  % akkaV    % "test",
        "com.typesafe.slick"  %%  "slick"         % "2.1.0",
        "io.spray"            %%  "spray-can"     % sprayV,
        "io.spray"            %%  "spray-json"    % "1.3.1",
        "io.spray"            %%  "spray-routing" % sprayV,
        "io.spray"            %%  "spray-testkit" % sprayV   % "test",
        "org.slf4j"           %   "slf4j-nop"     % "1.6.4",
        "org.specs2"          %%  "specs2-core"   % "2.3.11" % "test",
        "org.xerial"          %   "sqlite-jdbc"   % "3.7.2"            // << SQLite JDBC Driver
      )
    }
    

    2。在“MyService”特征中加载驱动程序:

     Class.forName("org.sqlite.JDBC") // load the sqlite jdbc driver (google for Class.forName)
     val db = Database.forURL(.....