简体   繁体   English

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

[英]How correctly connect to Oracle 12g database in Play Framework?

I am new in Play Framework (Scala) and need some advise.我是 Play Framework (Scala) 的新手,需要一些建议。

I use Scala 2.12 and Play Framework 2.6.20 .我使用Scala 2.12Play Framework 2.6.20 I need to use several databases in my project.我需要在我的项目中使用多个数据库。 Right now I connected MySQL database as it says in documentation.现在我按照文档中的说明连接了MySQL数据库。 How correctly connect project to remote Oracle 12g database?如何正确地将项目连接到远程Oracle 12g数据库?

application.conf:应用程序.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"
}

First of all to lib folder I put ojdbc8.jar file from oracle website.首先,我将ojdbc8.jar文件从oracle网站放到lib文件夹中。

Then add libraryDependencies += "com.oracle" % "ojdbc8" % "12.1.0.1" code to sbt.build file.然后将libraryDependencies += "com.oracle" % "ojdbc8" % "12.1.0.1"代码添加到sbt.build文件中。 Finally I wrote settings to aplication.conf file.最后我将设置写入aplication.conf文件。

After that step I notice error in terminal:在那一步之后,我注意到终端中的错误:

[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

EDIT :编辑

application.conf :应用程序.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"
}

ERROR :错误

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 : 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
    }
}

You cannot access Oracle without credentials.没有凭据,您将无法访问 Oracle。 You need to have an account with Oracle.您需要拥有 Oracle 帐户。 Then add something like the following to your build.sbt file然后将如下内容添加到build.sbt文件中

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

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

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

If you have the hard coded jar, you don't need to include as a dependency.如果您有硬编码的 jar,则不需要作为依赖项包含在内。 See unmanagedDependencies https://www.scala-sbt.org/1.x/docs/Library-Dependencies.html请参阅unmanagedDependencies https://www.scala-sbt.org/1.x/docs/Library-Dependencies.html

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

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