Skip to content

Instantly share code, notes, and snippets.

@tototoshi
Created December 23, 2011 04:10
Show Gist options
  • Save tototoshi/1513107 to your computer and use it in GitHub Desktop.
Save tototoshi/1513107 to your computer and use it in GitHub Desktop.
ScalaQueryでConnectionPoolを使う
/* build.sbt
libraryDependencies ++= Seq(
"org.scalaquery" % "scalaquery_2.9.0-1" % "0.9.5",
"postgresql" % "postgresql" % "8.4-702.jdbc4",
"commons-dbcp" % "commons-dbcp" % "1.4",
"com.github.seratch" %% "scalikejdbc" % "0.1.4" withSources ()
)
resolvers ++= Seq(
"seratch.github.com releases" at "http://seratch.github.com/mvn-repo/releases"
)
*/
import org.scalaquery._
import session.{ Database, Session }
import ql._
import basic.BasicDriver.Implicit._
import basic.{ BasicTable => Table }
import scalikejdbc.ConnectionPool
object Coffees extends Table[(Int, String, Int)]("coffees") {
def id = column[Int]("id")
def name = column[String]("name")
def price = column[Int]("price")
def * = id ~ name ~ price
}
object Example extends App {
ConnectionPool.initialize("jdbc:postgresql:scala_query_example",
user="toshi",
password="password")
val db = new Database {
override def createConnection(): java.sql.Connection = ConnectionPool.borrow()
}
db withSession { implicit s: Session =>
val q = for { c <- Coffees } yield c.name ~ c.price
q.list() foreach println
}
}
/*
> run
[info] Running Example
(Colombian,100)
(Espresso,200)
(Colombian_Decat,300)
[success] Total time: 0 s, completed 2011/12/23 13:08:02
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment