在 Play for Scala 独立应用程序中注入 JDBC 数据库连接

Injecting a JDBC Database connection in a Play for Scala standalone application

我在 Play for Scala 中注入 JDBC Database Connections 以在 Web 应用程序中使用 explained here:

这是我使用的声明:

class ScalaControllerInject @Inject()(db: Database) extends Controller {

    // rest of the code...

我需要的是在 standalone Play for Scala 应用程序(即没有控制器)中注入 JDBC 数据库连接,如下所示:

object Main extends App {

    val db: Database  = // ... get database 
    val conn = db.getConnection()

    // .... rest of the code
}

这可能吗?

您可以只创建一个(Postgres 示例):

val dbUrl = "jdbc:postgresql://localhost:5432/databaseName?user=username&password=yourpassword"
val database = Databases("org.postgresql.Driver",dbUrl,"testingzzz")