Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
169 lines (138 sloc) 5.91 KB

Adding rdbc to your project

rdbc is just an API — to actually use it as a database client you need an implementation called a driver. So — to use rdbc in your project you need to include two dependencies, one for the API, and one for a driver. This documentation doesn't describe any particular driver — you should be able to find artifact names you need to include in your build definition in the driver's documentation. For your convenience, however, Drivers page lists available drivers grouped by a database engine they support.

rdbc JARs are published to Maven Central repository. The API is currently available for Scala 2.11 and 2.12 and requires Java 8 runtime or newer.


For sbt projects, add the following to build.sbt:

libraryDependencies ++= Vector(
  "io.rdbc" %% "rdbc-api-scala" % "{{version}}",
  //here goes the driver dependency


For Gradle projects, add the following to the dependencies section of build.gradle:

Scala 2.12

compile group: 'io.rdbc', name: 'rdbc-api-scala_2.12', version: '{{version}}'
compile //here goes the driver dependency

Scala 2.11

compile group: 'io.rdbc', name: 'rdbc-api-scala_2.11', version: '{{version}}'
compile //here goes the driver dependency


For Maven projects, add the following to the dependencies element of pom.xml:

Scala 2.12

  <!-- here goes the driver dependency -->

Scala 2.11

  <!-- here goes the driver dependency -->

Working with Scala Futures

Since all rdbc API methods that perform I/O return Scala's Futures you'll need a knowledge on how to write asynchronous code using them. Throughout this documentation it is assumed that the reader has a basic knowledge about Future trait. If you're new to this concept you may find these resources useful:

A "Hello world" application

It's time for a small but complete example of rdbc API usage. A snippet below inserts a "Hello world!" greeting into a messages database table.

A table with the following definition is assumed to exist:

create table messages(txt varchar(100))

And here's the actual code snippet:

import io.rdbc.sapi._

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success}

object HelloRdbc extends App {

  val connFactory: ConnectionFactory = ???

  val greeting = "Hello World!"
  val insertFut: Future[Long] = connFactory.withConnection { conn =>
      .statement(sql"insert into messages(txt) values ($greeting)")
  }.andThen {
    case Success(count) => println(s"inserted $count greeting(s)")
    case Failure(ex) => ex.printStackTrace()

    insertFut.transformWith(_ => connFactory.shutdown()),

In this very simple application:

  • At line 10 a ConnectionFactory coming from a driver package should be instantiated. Each rdbc driver provides an implementation of this trait that allows to estabilish a connection to a database. ConnectionFactory implementation and classes needed for its configuration should be the only classes directly used from the driver package.

  • At line 13 a connection to the database is requested to be estabilished - when that happens, a function passed as a code block is executed.

  • In this code block, at line 15 a prepared statement is requested to be created using a sql string interpolator which passes a greeting string argument to it.

  • The statement is requested to be executed at line 16 and to return number of affected rows.

  • When the database operation finishes and the connection is released, at line 17 the Future's result is handled: number of affected rows is printed in case of a success or a stack trace is printed in case of an error.

  • None of the database calls block the executing thread - all of them return Futures that are then chained. The only statement that blocks starts at line 22 when the application waits for operations requested earlier to complete. Because this demo is just a simple console application, we need to block the thread somewhere to wait for a moment that the application can exit.


You can browse Scaladoc using site here. The site allows to switch between Scala versions and rdbc versions.