Giter Site home page Giter Site logo

jdub's Introduction

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, "[email protected]", "[email protected]"))

// or return the number of rows updated
db.update(UpdateUserEmail(4002, "[email protected]", "[email protected]"))

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

jdub's People

Contributors

codahale avatar ieure avatar swenson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.