如何使用 Scala 连接到 MySQL 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3077059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I connect to a MySQL database using Scala?
提问by Dan G
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.
我正在做一个小项目,我想在其中解析一些数据,然后将其放入数据库中。我没有与 Lift 合作,也找不到标准的方法来做到这一点。
I'm fine writing the queries myself, but I'm not sure what to use to actually connect to the DB.
我自己编写查询很好,但我不确定使用什么来实际连接到数据库。
回答by Brian Agnew
You can use JDBC- the standard means of getting Java to talk to databases. You'll need the appropriate MySQL JDBCdriver. Apache DbUtilsprovides some utility classes surrounding JDBC and would be useful.
您可以使用JDBC- 让 Java 与数据库对话的标准方法。您将需要适当的MySQL JDBC驱动程序。Apache DbUtils提供了一些围绕 JDBC 的实用程序类并且很有用。
If you want a higher level API which takes some of the boilerplate out, then check out Spring's JDBC integration.
如果你想要一个更高级别的 API 来去掉一些样板,然后查看Spring 的 JDBC 集成。
If you want an ORM (object-relational mapping), then Hibernateis a good choice.
如果你想要一个 ORM(对象关系映射),那么Hibernate是一个不错的选择。
I've used all three in Scala with success.
我已经成功地在 Scala 中使用了所有三个。
回答by Brian Clapper
I've actually written a SQL command shell, in Scala, that talks to any arbitrary database for which a JDBC driver exists. As Brian Agnew notes, it works perfectly. In addition, there are tools like Querulous, SQueryLand OR/Brokerthat provide Scala-friendly database layers. They sit on top of JDBC, but they provide some additional semantics (via DSLs, in some cases) to make things easier for you.
我实际上已经在 Scala 中编写了一个SQL 命令 shell,它可以与任何存在 JDBC 驱动程序的任意数据库进行通信。正如布赖恩·阿格纽 (Brian Agnew) 所指出的,它运行良好。此外,还有Querulous、SQueryL和OR/Broker等工具提供了对 Scala 友好的数据库层。它们位于 JDBC 之上,但它们提供了一些额外的语义(在某些情况下通过 DSL)使您的工作更轻松。
回答by Daniel Cukier
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.
当然,您可以使用所有与 JDBC 兼容的 Java 版本(Hibernate、Spring 等),但是为了更好地使用 Scala 语言,我建议使用具有更好 DSL 的 Scala 特定框架。
- ScalaQueryis 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:
- Squerylis a Scala ORM and DSL for talking with Databases with minimum verbosity and maximum type safety
- SORMis 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
- ScalaQuery是一种建立在 JDBC 之上的 API / DSL(领域特定语言),用于访问 Scala 中的关系数据库。它的设计考虑了以下目标:
- Squeryl是一个 Scala ORM 和 DSL,用于以最少的冗长和最大的类型安全与数据库交谈
- SORM是一个 Scala ORM 框架,旨在消除样板代码并通过高级抽象和函数式编程风格解决可扩展性问题
- Slick- 带有功能关系映射的类型安全支持的项目
Check out more about these frameworks at https://stackoverflow.com/questions/1362748/looking-for-a-comparison-of-scala-persistence-frameworks
在https://stackoverflow.com/questions/1362748/looking-for-a-comparison-of-scala-persistence-frameworks查看更多关于这些框架的信息
回答by nilskp
Try O/R Broker:
尝试O/R 代理:
case class MyObj(name: String, year: Int)
val ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource
// set properties on ds
import org.orbroker._
val builder = new BrokerBuilder(ds)
val broker = builder.build
val myObj: MyObj = // Parse stuff to create MyObj instance
broker.transaction() { session =>
session.execute("INSERT INTO MYTABLE VALUES(:obj.name, :obj.year)", "obj"->myObj)s
}
val myObjs: Seq[MyObj] = // Parse stuff to create sequence of MyObj instances
broker.transaction() { session =>
session.executeBatch("INSERT INTO MYTABLE VALUES(:obj.name, :obj.year)", "obj"->myObjs)
}
回答by DrGary
For completeness, also check out RichSQL. It's demo code showing how to wrap JDBC to make more Scala-like operations, but it's actually quite usable. It has the advantage of being simple and small, so you can easily study the source to see what's going on. Don't forget to close() your PreparedStatements.
为了完整起见,还可以查看RichSQL。这是一个演示代码,展示了如何包装 JDBC 以进行更多类似 Scala 的操作,但它实际上非常有用。它的优点是简单小巧,所以你可以很容易地研究源码,看看发生了什么。不要忘记 close() 你的 PreparedStatements。
回答by Peter Lamberg
I just discovered ScalikeJDBC which offers a Scala like API wrapper for JDBC.
我刚刚发现了ScalikeJDBC,它为 JDBC 提供了一个类似于 Scala 的 API 包装器。
(I found ScalikeJDBC when researching how to use ScalaAnormwithout Play Framework. Now it looks like I won't be needing Anorm for my project.)
(我发现ScalikeJDBC研究如何使用时ScalaAnorm无间隙框架。现在看来,我不会需要ANORM为我的项目。)
Here is a simple example, though it offers many interesting features not shown here:
这是一个简单的示例,尽管它提供了许多此处未显示的有趣功能:
import scalikejdbc._
Class.forName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource")
ConnectionPool.singleton("jdbc:mysql://localhost:3306/myschema", "user", "password")
DB.localTx { implicit conn =>
val data = sql"select mystringcol, myintcolumn from mytable".map {
rs => (rs.string("mystringcol"), rs.int("myintcolumn"))
}.list().apply()
println(data)
}
Some documentation links:
一些文档链接:

