我正在使用ScalaFX和MySQL数据库开发一个项目。
SBT通过build.sbt
文件成功添加了MySQL连接器。编译项目时,它会因类型不匹配错误而停止:
[error] found : com.aitrich.scalafx.test.DbConnection.type (with underlying type object com.aitrich.scalafx.test.DbConnection)
[error] required: com.aitrich.scalafx.test.DbConnection
[error] val orders: Seq[Person] = OrderDao.getAllOrders(dbc)
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 14 s, completed Nov 14, 2013 12:04:06 PM
以下是main方法的代码片段:
var dbc = DbConnection
val orders: Seq[Person] = OrderDao.getAllOrders(dbc)
这是DbConnection
案例类:
case class DbConnection() {
def getConnectionString =
"jdbc:mysql://%s:3306/simpleorder?user=%root&password=%sa".
format("localhost","root","sa")
}
为什么compile
会失败?
答案 0 :(得分:1)
tl; dr 您需要实例化(创建DbConnection
案例类的实例。
这绝不是SBT或ScalaFX的问题。
作为OrderDao.getAllOrders
方法的参数传递的是类型而不是类型的实例。这些类型根本不匹配,Scala编译器会破坏编译(这正是首先使用Scala的原因 - 在编译时进行彻底的类型检查)。
更改行
var dbc = DbConnection
到
var dbc = new DbConnection
并且编译器通过该行。请注意new
关键字。