from:http://mkaz.com/solog/scala/using-scala-with-jdbc-to-connect-to-mysql.html
Using Scala with JDBC to connect to MySQL
Date:may 27, 2011
A howto on connecting Scala to a MySQL database using JDBC. There is a number of database libraries for Scala, but I-ran into a problem getting most of the them to work. I attempted to use Scala.dbc, SCALA.DBC2, Scala Query and querulous but either they aren ' t supported, with a very limited Featured set or abstracts SQL to a weird pseudo language.
The Play Framework has a new database library called Anorm which tries to keep the interface to basic SQL but with a Sligh T improved Scala interface. The jury is still off for me, only used on one project minimally so far. Also, I ' ve only seen it work within a Play app, does don't look like it can be extracted out too easily.
So I ended up going with the basic Java JDBC connection and it turns out to being a fairly easy solution.
Here's the code for accessing a database using Scala and JDBC. You need to the connection string parameters and modify the query for your database. This example is geared towards MySQL, but a Java JDBC driver should work the same with Scala. Basic Query
Import java.sql. {Connection, DriverManager, ResultSet};
Change to Your Database Config
val conn_str = "jdbc:mysql://localhost:3306/dbname?user=dbuser&password= Dbpwd "
//Load The driver
Classof[com.mysql.jdbc.driver]
//Setup The connection
val conn = Drivermanager.getconnection (CONN_STR)
try {
//Configure to is Read only
val statement = Conn.createstatement (resultset.type_forward_only, resultset.concur_read_only)
//Execute Query
val rs = Statement.executequery ("Select quote from quotes LIMIT 5")
//Iterate over ResultSet while
(rs.next) {
println (rs.getstring ("quote")
}}
finally {
conn.close
}
You'll need to download the Mysql-connector jar. Download the MySQL connector jar from here.
Or If you're using MAVEN, the POM snippets to load the MySQL connector, and you'll need to check out what the latest version is.
<dependency>
<groupId>mysql</groupId>
<artifactid>mysql-connector-java</ artifactid>
<version>5.1.12</version>
</dependency>
To run the example, save the following to a file (Query_test.scala) and run using, the following specifying the CLASSPATH To the connector jar:
SCALA-CP Mysql-connector-java-5.1.12.jar:. Query_test.scala
Insert, Update and Delete
To perform a insert, update or delete need to the Create an updatable statement object. The execute command is slightly different and you'll most likely want to use some sort of parameters. Here's an example doing a insert using JDBC and Scala with parameters.
//CREATE database connection val DBC = "Jdbc:mysql://localhost:3306/dbname?user=dbuser&password=dbpwd" CLA Ssof[com.mysql.jdbc.driver] Val conn = drivermanager.getconnection (DBC) val statement = conn.createstatement (ResultSet . Type_forward_only, resultset.concur_updatable)//Do database insert try {val prep = conn.preparestatement ("Inse RT into quotes (quote, author) VALUES (?,?)
") prep.setstring (1," Nothing great is ever achieved without enthusiasm. ") Prep.setstring (2, "Ralph Waldo Emerson") Prep.executeupdate} finally {Conn.close}