这是我遇到的错误:
[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
在java error (No suitable driver found)我发现:
>您需要使用Class.forName(“org.postgresql.Driver”)将驱动程序加载到某个地方;
>您将在程序的类路径中使用Postgresql驱动程序的jar文件。
在Application.scala中,我有以下代码块:
{ println(ConfigFactory.load().getString("db.default.url")) println(Class.forName("org.postgresql.Driver")) }
重播运行编译结果:
(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]
然后我运行播放依赖和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 )
我的/ lib文件中还有postgresql-9.2-1002.jdbc4.jar和slick_2.10-1.0.1-RC1.jar,我的本地Postgres版本从做一个SELECT version();是9.2.4 Postgres驱动程序的分辨率似乎解决了9.1 .jar,但是当我注释到应用程序的依赖关系让/ lib被包含在自己的位置时,/ lib似乎不在Play的CLASSPATH中。
我知道Postgres url是正确的,当我的应用程序第一次启动时,我可以连接到我的数据库。
So then I looked at this question which says…
@H_404_51@
- You’ll need to load the driver somewhere. Class.forName(“org.postgresql.Driver”);
- You’ll need the postgresql driver .jar file in the classpath of your program.
在这种情况下不适用。你有一个框架,为你照顾这些东西。您提到的这个问题是描述如何使用“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,"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)) } }