|
Play Framework/Scala example source code file (H2Database.scala)
The H2Database.scala Play Framework example source codepackage anorm import java.sql.{ DriverManager, Connection } import scala.util.Random trait H2Database { def withConnection[R](block: Connection => R) = { val dbName = "test" + Random.alphanumeric.take(6).mkString("") Class.forName("org.h2.Driver") val connection = DriverManager.getConnection("jdbc:h2:mem:" + dbName, "sa", "") try { block(connection) } finally { connection.close() } } case class TestTable(id: Long, foo: String, bar: Int) /** Create a simple 'test1' table for testing with. */ def createTest1Table()(implicit conn: Connection): Unit = createTable("test1", "id bigint", "foo varchar", "bar int") /** Create a simple 'test2' table for testing with. */ def createTest2Table()(implicit conn: Connection): Unit = createTable("test2", "id bigint", "foo varchar") private def createTable(name: String, columns: String*)(implicit conn: Connection): Unit = conn.createStatement().execute("create table " + name + " ( " + columns.mkString(", ") + ");") } Other Play Framework source code examplesHere is a short list of links related to this Play Framework H2Database.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.