Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3321792
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7194
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27








分类: Mysql/postgreSQL

2012-12-03 22:35:16

A howto on connecting Scala to a MySQL database using JDBC. There are a number of database libraries for Scala, but I ran into a problem getting most of them to work. I attempted to use scala.dbc, scala.dbc2, Scala Query and Querulous but either they aren't supported, have 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 slight improved scala interface. The jury is still out for me, only used on one project minimally so far. Also, I've only seen it work within a Play app, does not look like it can be extracted out too easily.

So I ended up going with basic Java JDBC connection and it turns out to be a fairly easy solution.

Here is the code for accessing a database using Scala and JDBC. You need to change the connection string parameters and modify the query for your database. This example was geared towards MySQL, but any Java JDBC driver should work the same with Scala.

Basic Query

  1. import java.sql.{Connection, DriverManager, ResultSet};

  2. // Change to Your Database Config
  3. val conn_str = "jdbc:mysql://localhost:3306/DBNAME?user=DBUSER&password=DBPWD"

  4. // Load the driver
  5. classOf[com.mysql.jdbc.Driver]

  6. // Setup the connection
  7. val conn = DriverManager.getConnection(conn_str)
  8. try {
  9. // Configure to be Read Only
  10. val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)

  11. // Execute Query
  12. val rs = statement.executeQuery("SELECT quote FROM quotes LIMIT 5")

  13. // Iterate Over ResultSet
  14. while ( {
  15. println(rs.getString("quote"))
  16. }
  17. }
  18. finally {
  19. conn.close
  20. }
You will need to download the mysql-connector jar Or if you are using maven, the pom snippets to load the mysql connector, you'll need to check what the latest version is.

  1. mysql
  2. mysql-connector-java
  3. 5.1.12
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:

  1. scala -cp mysql-connector-java-5.1.12.jar:. query_test.scala

Insert, Update and Delete

To perform an insert, update or delete you need to create an updatable statement object. The execute command is slightly different and you will most likely want to use some sort of parameters. Here's an example doing an insert using jdbc and scala with parameters.

  1. // create database connection
  2. val dbc = "jdbc:mysql://localhost:3306/DBNAME?user=DBUSER&password=DBPWD"
  3. classOf[com.mysql.jdbc.Driver]
  4. val conn = DriverManager.getConnection(dbc)
  5. val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)

  6. // do database insert
  7. try {
  8. val prep = conn.prepareStatement("INSERT INTO quotes (quote, author) VALUES (?, ?) ")
  9. prep.setString(1, "Nothing great was ever achieved without enthusiasm.")
  10. prep.setString(2, "Ralph Waldo Emerson")
  11. prep.executeUpdate
  12. }
  13. finally {
  14. conn.close
  15. }


阅读(1920) | 评论(1) | 转发(0) |