【问题标题】:How correctly connect to Oracle 12g database in Play Framework?在 Play Framework 中如何正确连接到 Oracle 12g 数据库?
【发布时间】:2018-11-12 12:10:55
【问题描述】:

我是 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"
}

首先到lib文件夹我把ojdbc8.jar文件从oracle网站。

然后将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
    }
}

【问题讨论】:

  • 你已经尝试过什么?分享代码。
  • @cchantep 你能再看看我的帖子吗?我补充一些信息。看来我使用了不正确的 .jar 文件版本。
  • @cchantep 你有什么想法吗?
  • 检查您的构建,没有特定于 Play 的内容

标签: database oracle scala playframework


【解决方案1】:

没有凭据就无法访问 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,则不需要包含为依赖项。见unmanagedDependencieshttps://www.scala-sbt.org/1.x/docs/Library-Dependencies.html

【讨论】:

  • 感谢您的回答。我有问题。我添加了您推荐的代码,但不幸的是,steal 有同样的错误。这是否意味着我不再需要 build.sbt 文件中的 libraryDependencies += "com.oracle" % "ojdbc8" % "12.1.0.1" 了?
  • 我在终端也注意到这个错误信息:[error] Unable to find credentials for [OAM 11g @ login.oracle.com].
  • 是的。要么,要么,如果你有罐子,你不需要把它放在libraryDependencies。你不需要两者都做。
  • 我想你找到了。添加credentials += Credentials("OAM 11g", "login.oracle.com", "username", "password")
  • 我只将您推荐的代码添加到build.sbt 文件中,而没有libraryDependencies。现在,当我尝试从数据库中获取任何数据时,它会引发错误。从错误中我了解到 Play Framework 和远程 Oracle 数据库之间没有任何连接。你能再看看我的帖子吗?!
猜你喜欢
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 2016-11-05
  • 2010-09-10
相关资源
最近更新 更多