Documenting the code that works for me below:
Entry to include in dependencies in build.sbt
:
// https://mvnrepository.com/artifact/org.playframework.anorm/anorm
libraryDependencies += "org.playframework.anorm" %% "anorm" % "2.6.7"
Write helper classes:
@Singleton
class DBUtils {
val schema = AppConfig.defaultSchema
def withDefaultConnection(sqlQuery: SqlQuery) = {
// could replace with DBCP, not got a chance yet
val conn = DriverManager.getConnection(AppConfig.dbUrl,AppConfig.dbUser, AppConfig.dbPassword)
val result = Try(sqlQuery.execute()(conn))
conn.close()
result
}
}
object DBUtils extends DBUtils
Next, any query can use the withDefaultConnection
method to execute:
def saveReviews(listOfReviews: List[Review]):Try[Boolean]= {
val query = SQL(
s"""insert into aws.reviews
| ( reviewerId,
| asin,
| overall,
| summary,
| unixReviewTime,
| reviewTime
| )
|values ${listOfReviews.mkString(",")}""".stripMargin)
//println(query.toString())
DBUtils.withDefaultConnection(query)
}
c
that you are supplying is the database connection object, which is ajava.sql.Connection
. You can supply a real connection and provide it as an implicit. – Regression