scala调用jdbc连接数据库

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 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

  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 be 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 will need to download the mysql-connector jar. Download the mysql connector jar from here.

Or if you are using maven, the pom snippets to load the mysql connector, you'll need to check 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 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.

  // create database connection
  val dbc = "jdbc:mysql://localhost:3306/DBNAME?user=DBUSER&password=DBPWD"
  classOf[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("INSERT INTO quotes (quote, author) VALUES (?, ?) ")
    prep.setString(1, "Nothing great was ever achieved without enthusiasm.")
    prep.setString(2, "Ralph Waldo Emerson")
    prep.executeUpdate
  }
  finally {
    conn.close
  }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值