Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect to a MySQL database using Scala?

Tags:

mysql

scala

I'm working on a little project where I'd like to parse some data, and then put it into a database. I'm not working with Lift, and I haven't been able to find a standard way to do this.

I'm fine writing the queries myself, but I'm not sure what to use to actually connect to the DB.

like image 593
Dan G Avatar asked Jun 19 '10 20:06

Dan G


2 Answers

You can use JDBC - the standard means of getting Java to talk to databases. You'll need the appropriate MySQL JDBC driver. Apache DbUtils provides some utility classes surrounding JDBC and would be useful.

If you want a higher level API which takes some of the boilerplate out, then check out Spring's JDBC integration.

If you want an ORM (object-relational mapping), then Hibernate is a good choice.

I've used all three in Scala with success.

like image 124
Brian Agnew Avatar answered Sep 28 '22 03:09

Brian Agnew


Off course you can use all Java version compatible with JDBC (Hibernate, Spring, etc), but for better use of Scala language, I recommend using a Scala specific framework, which have a better DSL.

  • ScalaQuery is an API / DSL (domain specific language) built on top of JDBC for accessing relational databases in Scala. It was designed with the following goals in mind:
  • Squeryl is a Scala ORM and DSL for talking with Databases with minimum verbosity and maximum type safety
  • SORM is a Scala ORM-framework designed to eliminate boilerplate code and solve the problems of scalability with a high level abstraction and a functional programming style
  • Slick - Typesafe backed project with Functional Relational Mapping

Check out more about these frameworks at https://stackoverflow.com/questions/1362748/looking-for-a-comparison-of-scala-persistence-frameworks

like image 40
Daniel Cukier Avatar answered Sep 28 '22 02:09

Daniel Cukier