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
<dependency>
<groupId>io.rdbc</groupId>
<artifactId>rdbc-api-scala_2.12</artifactId>
<version>{{version}}</version>
</dependency>
<dependency>
<!-- here goes the driver dependency -->
</dependency>
Scala 2.11
<dependency>
<groupId>io.rdbc</groupId>
<artifactId>rdbc-api-scala_2.11</artifactId>
<version>{{version}}</version>
</dependency>
<dependency>
<!-- here goes the driver dependency -->
</dependency>
Since all rdbc API methods that perform I/O return Scala's Future
s 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:
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:
#!scala
import io.rdbc.sapi._
import scala.concurrent.ExecutionContext.Implicits.global
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 =>
conn
.statement(sql"insert into messages(txt) values ($greeting)")
.executeForRowsAffected()
}.andThen {
case Success(count) => println(s"inserted $count greeting(s)")
case Failure(ex) => ex.printStackTrace()
}
Await.ready(
insertFut.transformWith(_ => connFactory.shutdown()),
10.seconds
)
}
In this very simple application:
-
At line
10
aConnectionFactory
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 asql
string interpolator which passes agreeting
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
theFuture
'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
Future
s that are then chained. The only statement that blocks starts at line22
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 javadoc.io site here. The site allows to switch between Scala versions and rdbc versions.