How to use Anorm outside the game?

How do you use Anorm outside of Scala? In the Anorm document for the game, it just uses something like:

DB.withConnection { implicit c => val result: Boolean = SQL("Select 1").execute() } 

The DB object is for playback only. How do you use Anorm yourself without using Play?

+7
scala playframework anorm
source share
2 answers

There is no need for a DB object (part of Play JDBC, not Anorm). Anorm works the way you mean it, as implicit:

 implicit val con: java.sql.Connection = ??? // whatever you want to resolve connection SQL"SELECT * FROM Table".as(...) 

You can enable the JDBC connection in many ways: basic DriverManager.getConnection , JNDI, ...

Regarding the dependency, it's easy to add it to the SBT: How to declare a dependency on an anonymous announcement for a single application? .

+12
source share

You can also emulate a DB object as follows (I have not tried this yet)

  object DB { def withConnection[A](block: Connection => A): A = { val connection: Connection = ConnectionPool.borrow() try { block(connection) } finally { connection.close() } } } 

Taken from https://github.com/TimothyKlim/anorm-without-play/blob/master/src/main/scala/Main.scala

+1
source share

All Articles