A damn simple JDBC wrapper. Y'know. For databases.
Scala Java
Pull request Compare This branch is 100 commits behind SimpleFinance:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE.md
README.md
pom.xml

README.md

jdub

A damn simple JDBC wrapper. Y'know. For databases.

Requirements

  • Java SE 6
  • Scala 2.9.2

How To Use

First, specify Jdub as a dependency:

<dependencies>
  <dependency>
    <groupId>com.simple</groupId>
    <artifactId>jdub_${scala.version}</artifactId>
    <version>0.1.0</version>
  </dependency>
</dependencies>

(Don't forget to include your JDBC driver!)

Second, connect to a database:

val db = Database.connect("jdbc:postgresql://localhost/wait_what", "myaccount", "mypassword")

Third, run some queries:

case class GetUser(userId: Long) extends Query[Option[User]] {
  val sql = trim("""
SELECT id, email, name
  FROM users
 WHERE id = ?
""")

  val values = userId :: Nil
  
  def reduce(results: Iterator[Row]) = {
    for (row <- results;
         id <- row.long("id");
         email <- row.string("email");
         name <- row.string("name"))
      yield User(id, email, name)
  }.toStream.headOption
}

// this'll print the user record for user #4002
println(db(GetUser(4002)))

Fourth, execute some statements:

case class UpdateUserEmail(userId: Long, oldEmail: String, newEmail: String) extends Statement {
  val sql = trim("""
UPDATE users
   SET email = ?
 WHERE userId = ? AND email = ?
""")

  val values = userId :: oldEmail :: newEmail :: Nil
}

// execute the statement
db.execute(UpdateUserEmail(4002, "old@example.com", "new@example.com"))

// or return the number of rows updated
db.update(UpdateUserEmail(4002, "old@example.com", "new@example.com"))

License

Copyright (c) 2011-2012 Coda Hale Copyright (c) 2012-2013 Simple Finance Technology Corp. All rights reserved.

Published under The MIT License, see LICENSE.md