First steps with JDBC
Let's start by connecting to JDBC from the command line. To follow with the examples, you will need access to a running MySQL server. If you added the MySQL connector to the list of dependencies, open a Scala console by typing the following command:
$ sbt console
Let's import JDBC:
scala> import java.sql._ import java.sql._
We then need to tell JDBC to use a specific connector. This is normally done using reflection, loading the driver at runtime:
scala> Class.forName("com.mysql.jdbc.Driver") Class[_] = class com.mysql.jdbc.Driver
This loads the appropriate driver into the namespace at runtime. If this seems somewhat magical to you, it's probably not worth worrying about exactly how this works. This is the only example of reflection that we will consider in this book, and it is not particularly idiomatic Scala.
Connecting to a database server
Having specified the SQL connector, we can now connect to a database. Let's assume that we have a database called test
on...