ps0604
ps0604

Reputation: 1081

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

I inject JDBC Database Connections in Play for Scala to use in web applications as explained here:

This is the declaration I use:

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

    // rest of the code...

What I need is to inject the JDBC Database Connection in a standalone Play for Scala application (i.e. there's no Controller), like so:

object Main extends App {

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

    // .... rest of the code
}

Is this possible?

Upvotes: 0

Views: 266

Answers (1)

Tomer
Tomer

Reputation: 2448

You can just create one (Postgres example):

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

Upvotes: 2

Related Questions