我正在使用本地 Postgres 数据库和Slick 1.0.0 使用Play Framework 2.1.1 编写 Scala Web 应用程序,我在这里遇到了似乎矛盾的问题。
这是我遇到的错误:
[SQLException: No suitable driver found for postgres://user:password@localhost:5432/postgres]
56
57 def instance = Action {
58 Database.forURL("postgres://user:password@localhost:5432/postgres", driver = "org.postgresql.Driver") withSession {
59 val q = Retailer.map(_.name)
60 Ok(views.html.instance(q.list, newRForm))
61 }
62 }
63
Whereuser
和password
分别是 Postgres 数据库的username
和password
。
在java 错误(未找到合适的驱动程序)中,我发现:
- 您需要使用某处加载驱动程序
Class.forName("org.postgresql.Driver");
- 您需要在程序的类路径中安装 PostgreSQL 驱动程序的 jar 文件。
在Application.scala
我有以下代码块:
{
println(ConfigFactory.load().getString("db.default.url"))
println(Class.forName("org.postgresql.Driver"))
}
重新运行play compile
结果:
(Server started, use Ctrl+D to stop and go back to the console...)
[info] play - database [default] connected at jdbc:postgresql://localhost:5432/postgres
[info] play - Application started (Dev)
postgres://user:password@localhost:5432/postgres
class org.postgresql.Driver
[error] application -
! @6ei1nhkop - Internal server error, for (GET) [/instance] ->
play.api.Application$$anon$1: Execution exception[[SQLException: No suitable driver found for jdbc:postgresql://user:password@localhost:5432/postgres]]
at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.1]
at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$17$$anonfun$apply$24.apply(PlayDefaultUpstreamHandler.scala:326) [play_2.10.jar:2.1.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$17$$anonfun$apply$24.apply(PlayDefaultUpstreamHandler.scala:324) [play_2.10.jar:2.1.1]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1]
java.sql.SQLException: No suitable driver found for jdbc:postgresql://user:password@localhost:5432/postgres
at java.sql.DriverManager.getConnection(Unknown Source) ~[na:1.7.0_21]
at java.sql.DriverManager.getConnection(Unknown Source) ~[na:1.7.0_21]
at scala.slick.session.Database$$anon$2.createConnection(Database.scala:105) ~[slick_2.10-1.0.0.jar:1.0.0]
at scala.slick.session.BaseSession.conn$lzycompute(Session.scala:207) ~[slick_2.10-1.0.0.jar:1.0.0]
at scala.slick.session.BaseSession.conn(Session.scala:207) ~[slick_2.10-1.0.0.jar:1.0.0]
at scala.slick.session.BaseSession.close(Session.scala:221) ~[slick_2.10-1.0.0.jar:1.0.0]
然后我运行play dependencies
,postgres .jar 就解决了!
Here are the resolved dependencies of your application:
+--------------------------------------------------------+--------------------------------------------------------+-----------------------------------+
| ←[32mpostgresql:postgresql:9.1-901-1.jdbc4←[0m | ←[37mats:ats_2.10:1.0-SNAPSHOT←[0m | ←[37mAs postgresql-9.1-901-1.jdbc4.jar←[0m |
+--------------------------------------------------------+--------------------------------------------------------+-----------------------------------+
为什么找不到合适的驱动程序?
conf/application.conf
# Database configuration
db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgres://user:password@localhost:5432/postgres"
db.default.user=user
db.default.password=password
项目/Build.scala
import sbt._
import Keys._
import play.Project._
object ApplicationBuild extends Build {
val appName = "ats"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
// Add your project dependencies here,
jdbc,
"com.typesafe.slick" %% "slick" % "1.0.0",
"postgresql" % "postgresql" % "9.1-901-1.jdbc4"
)
val main = play.Project(appName, appVersion, appDependencies).settings(
// Add your own project settings here
)
我也有postgresql-9.2-1002.jdbc4.jar
andslick_2.10-1.0.1-RC1.jar
在我的/lib
文件中,我的本地 Postgres 版本SELECT version();
是9.2.4虽然 postgres 驱动程序解析似乎正在解析为 9.1 .jar,但当我注释掉应用程序依赖项以/lib
使其独立包含时,/lib
似乎不在 Play 的 CLASSPATH 上。
我知道 Postgres url 是正确的,并且当我的应用程序首次启动时,我能够连接到我的数据库。