如何在Play Framework中正确连接到Oracle 12g数据库?

时间:2018-11-10 10:13:05

标签: database oracle scala playframework

我是Play Framework(Scala)的新手,需要一些建议。

我使用Scala 2.12Play Framework 2.6.20。我需要在项目中使用多个数据库。现在,我按照文档中的说明连接了MySQL数据库。如何正确将项目连接到远程Oracle 12g数据库?

application.conf:

db {
  mysql.driver = com.mysql.cj.jdbc.Driver
  mysql.url = "jdbc:mysql://host:port/database?characterEncoding=UTF-8"
  mysql.username = "username"
  mysql.password = "password"
}

首先,我将oracle网站上的lib文件放到ojdbc8.jar文件夹中。

然后将libraryDependencies += "com.oracle" % "ojdbc8" % "12.1.0.1"代码添加到sbt.build文件中。最后,我将设置写入了aplication.conf文件。

那一步之后,我发现终端错误:

[error] (*:update) sbt.ResolveException: unresolved dependency: com.oracle#ojdbc8;12.1.0.1: not found
[error] Total time: 6 s, completed 10.11.2018 16:48:30
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256M; support was removed in 8.0

编辑

application.conf

db {
  mysql.driver = com.mysql.cj.jdbc.Driver
  mysql.url = "jdbc:mysql://@host:@port/@database?characterEncoding=UTF-8"
  mysql.username = "@username"
  mysql.password = "@password"

  oracle.driver = oracle.jdbc.driver.OracleDriver
  oracle.url = "jdbc:oracle:thin:@host:@port/@sid"
  oracle.username = "@username"
  oracle.password = "@password"
}

错误

play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

1) No implementation for play.api.db.Database was bound.
  while locating play.api.db.Database
    for the 1st parameter of controllers.GetMarkersController.<init>(GetMarkersController.scala:14)
  while locating controllers.GetMarkersController
    for the 7th parameter of router.Routes.<init>(Routes.scala:45)
  at play.api.inject.RoutesProvider$.bindingsFromConfiguration(BuiltinModule.scala:121):
Binding(class router.Routes to self) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)

GetMarkersController.scala

package controllers

import javax.inject._

import akka.actor.ActorSystem
import play.api.Configuration
import play.api.mvc.{AbstractController, ControllerComponents}
import play.api.libs.ws._
import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future, Promise}
import services._
import play.api.db.Database

class GetMarkersController @Inject()(db: Database, conf: Configuration, ws: WSClient, cc: ControllerComponents, actorSystem: ActorSystem)(implicit exec: ExecutionContext) extends AbstractController(cc) {
    def getMarkersValues(start_date: String, end_date: String) = Action.async {
        getValues(1.second, start_date: String, end_date: String).map {
            message => Ok(message)
        }
    }

    private def getValues(delayTime: FiniteDuration, start_date: String, end_date: String): Future[String] = {
        val promise: Promise[String] = Promise[String]()
        val service: GetMarkersService = new GetMarkersService(db)
        actorSystem.scheduler.scheduleOnce(delayTime) {
            promise.success(service.get_markers(start_date, end_date))
        }(actorSystem.dispatcher)
        promise.future
    }
}

1 个答案:

答案 0 :(得分:0)

没有凭据就无法访问Oracle。您需要拥有一个Oracle帐户。然后将以下内容添加到您的build.sbt文件

resolvers += "Oracle" at "https://maven.oracle.com"

credentials += Credentials("Oracle", "maven.oracle.com", "username", "password")

有关访问OTN的更多信息:https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9012

如果您具有硬编码的jar,则不需要将其包含为依赖项。参见unmanagedDependencies https://www.scala-sbt.org/1.x/docs/Library-Dependencies.html