scala-jdbc


License

License

Categories

Categories

Scala Languages
GroupId

GroupId

com.github.takezoe
ArtifactId

ArtifactId

scala-jdbc_2.11
Last Version

Last Version

1.0.5
Release Date

Release Date

Type

Type

jar
Description

Description

scala-jdbc
scala-jdbc
Project URL

Project URL

https://github.com/takezoe/scala-jdbc
Project Organization

Project Organization

com.github.takezoe
Source Code Management

Source Code Management

https://github.com/takezoe/scala-jdbc

Download scala-jdbc_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.takezoe/scala-jdbc_2.11/ -->
<dependency>
    <groupId>com.github.takezoe</groupId>
    <artifactId>scala-jdbc_2.11</artifactId>
    <version>1.0.5</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.takezoe/scala-jdbc_2.11/
implementation 'com.github.takezoe:scala-jdbc_2.11:1.0.5'
// https://jarcasting.com/artifacts/com.github.takezoe/scala-jdbc_2.11/
implementation ("com.github.takezoe:scala-jdbc_2.11:1.0.5")
'com.github.takezoe:scala-jdbc_2.11:jar:1.0.5'
<dependency org="com.github.takezoe" name="scala-jdbc_2.11" rev="1.0.5">
  <artifact name="scala-jdbc_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.takezoe', module='scala-jdbc_2.11', version='1.0.5')
)
libraryDependencies += "com.github.takezoe" % "scala-jdbc_2.11" % "1.0.5"
[com.github.takezoe/scala-jdbc_2.11 "1.0.5"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.8
com.github.jsqlparser : jsqlparser jar 0.9.6
org.scalamacros : resetallattrs_2.11 jar 1.0.0
org.scala-lang : scala-reflect jar 2.11.8

provided (1)

Group / Artifact Type Version
org.scala-lang : scala-compiler jar 2.11.8

test (2)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.1
com.h2database : h2 jar 1.4.192

Project Modules

There are no modules declared in this project.

scala-jdbc Actions Status

Better JDBC wrapper for Scala.

libraryDependencies += "com.github.takezoe" %% "scala-jdbc" % "1.0.5"

You can use better-jdbc by adding a following import statements:

import com.github.takezoe.scala.jdbc._

Select

Extract values from ResultSet:

val users: Seq[(Int, String)] = DB.autoClose(conn) { db =>
  db.select("SELECT * FROM USERS"){ rs =>
    (rs.getInt("USER_ID"), rs.getString("USER_NAME"))
  }
}

Retrieve a first record:

val user: Option[(Int, String)] = DB.autoClose(conn) { db =>
  db.selectFirst(sql"SELECT * FROM USERS WHERE USER_ID = $userId"){ rs =>
    (rs.getInt("USER_ID"), rs.getString("USER_NAME"))
  }
}

Map ResultSet to the case class:

case class User(userId: Int, userName: String)

val users: Seq[User] = DB.autoClose(conn) { db =>
  db.select("SELECT USER_ID, USER_NAME FROM USERS", User.apply _)
}

Update

DB.autoClose(conn) { db =>
  db.update(sql"INSERT INTO USERS (USER_ID, USER_NAME) VALUES ($userId, $userName)")
}

Transaction

DB.autoClose(conn) { db =>
  db.transaction {
    db.update(sql"DELETE FROM GROUP WHERE USER_ID = $userId")
    db.update(sql"DELETE FROM USERS WHERE USER_ID = $userId")
  }
}

Large data

Process large data using scan method:

DB.autoClose(conn) { db =>
  db.scan("SELECT * FROM USERS"){ rs =>
    println(rs.getString("USER_NAME"))
  }
}

SQL Validation (Experimental)

scala-jdbc provides sqlc macro that validates a given SQL. You can use it instead of sql string interpolation.

db.selectFirst(sqlc(s"SELECT * FROM USERS WHERE USER_ID = $userId")){ rs =>
  (rs.getInt("USER_ID"), rs.getString("USER_NAME"))
}

This macro checks the sql syntax using JsqlParser. When a given SQL is invalid, errors are reported in compile time.

Versions

Version
1.0.5
1.0.4
1.0.3
1.0.2