我是 java/scala 生态系统的 super 菜鸟,但我在让这个示例项目正常工作时遇到了困难。
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合适的驱动程序找到jdbc:sqlite:./test.sqlite3
错误。显然我没有正确加载驱动程序,但我在互联网上搜索了正确的语法,但悲惨地找不到任何东西。我错过了什么?
谢谢!
附: Scala 2.11.2、Spray 1.3.2、Slick 2.1.0。
最佳答案
- 您需要将 sqlite jdbc 驱动程序添加到您的依赖项中:
将您的 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(.....
关于scala - Spray.io、光滑、sqlite3 : no suitable driver found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044946/