简体   繁体   English

Play Framework 2.6中的JDBC连接

[英]JDBC connection in Play Framework 2.6

I am using Play-2.6 and want to connect Play application to postgreSQL. 我正在使用Play-2.6,并想将Play应用程序连接到postgreSQL。

In application.conf file I have application.conf文件中

play.db { 

  # The combination of these two settings results in "db.default" as the
  # default JDBC pool:
  config = "db"
  default = "default"

  default.driver = org.postgresql.Driver
  default.url = jdbc:postgresql://localhost:5432/mydb1
  default.username = "postgres"
  default.password = "postgres"

} 

In build.sbt file I have build.sbt文件中

libraryDependencies += jdbc
libraryDependencies +="org.postgresql" % "postgresql" % "9.4-1206-jdbc42" <br/>

In a service class RadioParameters.scala I am trying to access the DB using 在服务类RadioParameters.scala我尝试使用

import models._
import play.api.db._
import java.sql._
import java.sql.Connection
import java.sql.DriverManager._


class RadioParameters(_ap_mac_id:String) {
         val ap_mac_id =_ap_mac_id

  def radioparameters:radio = {

                **val connection =  DB.getConnection()**
                .....
  }

and I am getting error: 我得到错误:

**\RadioParameters.scala:38:35: not found: value DB
val connection =  DB.getConnection() <br/>**

I tried replacing it with Database, db but nothing seems to work. 我尝试用数据库,数据库替换它,但似乎没有任何效果。 I have seen accessing DB like this somewhere on web. 我曾经在网络上的某个地方看到过这样的数据库访问。 I checked play-jdbc-api_2.12-2.6.7 in Eclipse.It doesn't contain any DB class or object. 我在play-jdbc-api_2.12-2.6.7中检查了play-jdbc-api_2.12-2.6.7 。它不包含任何数据库类或对象。

How do I get this thing working ? 我如何使这件事起作用?
Any kind of link or suggestion is totally appreciable. 任何形式的链接或建议都是完全可以理解的。

Play 2.6 uses dependency injection and DB is not available anymore, try something like: Play 2.6使用依赖注入,并且DB不再可用,请尝试以下操作:

@Singleton
class RadioParameters @Inject()(db: DBApi) {
  val dbName = "default"

  def radioparameters():radio = {
    db.database(dbName).withConnection(implicit connection =>
      //db call
    )
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM