我正在使用Play Framework 2.1.1使用本地Postgres数据库和Slick 1.0.0编写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
user
和password
分别是Postgres数据库的username
和password
。
在java error (No suitable driver found)我找到了:
Class.forName("org.postgresql.Driver");
在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
文件中也有slick_2.10-1.0.1-RC1.jar
和/lib
,而SELECT version();
的本地Postgres版本 9.2.4 postgres驱动程序解决方案似乎正在解决9.1 .jar,当我注释掉应用程序依赖项以让/lib
独立时,/lib
似乎没有玩CLASSPATH。
我知道Postgres网址是正确的,我可以在应用程序首次启动时连接到我的数据库。
答案 0 :(得分:16)
你正在混淆。
那么我看了一下这个问题......
- 您需要在某处加载驱动程序。的Class.forName( “org.postgresql.Driver”);
- 您需要在程序的类路径中使用postgresql驱动程序.jar文件。
醇>
这不适用于这种情况。你有一个框架,可以为你处理这些事情。您提到的问题是描述如何使用“raw”jdbc访问数据库。
这是你应该怎么做的。
首先,您可以简化配置部分。 5432
是postresql的默认端口,localhost
也是默认主机。用户名和密码应放在网址之外。
# Database configuration
db.default.driver=org.postgresql.Driver
db.default.url=jdbc:postgres:postgres
db.default.user=user
db.default.password=password
现在定义适当的sbt依赖项。只需从/lib
文件夹中删除jar文件,然后通过更改Build.scala
appDependencies更新您的依赖项以获取最新的PostgreSQL驱动程序(9.2)。请注意,groupId已从postgresql
更改为org.postgresql
。
val appDependencies = Seq(
// Add your project dependencies here,
jdbc,
"com.typesafe.slick" %% "slick" % "1.0.0",
"org.postgresql" % "postgresql" % "9.3-1102-jdbc41"
)
最后,您应该更改控制器以从配置中解析数据源:
def instance = Action {
Database.forDataSource(DB.getDataSource()) withSession {
val q = Retailer.map(_.name)
Ok(views.html.instance(q.list, newRForm))
}
}