1

我刚刚开始使用 Scala 和提升框架工作。我正在尝试使用普通 JDBC 连接我的数据库。我希望从 default.props 文件中读取我的数据库凭据。到目前为止,我尝试了以下代码:在我的 default.props 文件中:

db.class=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost/scalatest
db.user=root
db.password=

在我的 boot.scala 文件中,我尝试像这样进行 JDBC 连接:

val filename = "/src/main/resources/props/default.props"
Props.whereToLook = () => ((filename, () => Full(new FileInputStream(filename))) :: Nil)

val DBDriver = Props.get("db.class").toString
val DBURL =  Props.get("db.url").toString
val DBUsrName =  Props.get("db.user").toString
val DBPass = Props.get("db.password").toString

Class.forName(DBDriver)
val conn  = DriverManager.getConnection(DBURL,DBUsrName, DBPass)

但是在使用 container:start 命令运行服务器时,会显示 fileNotFoundException。谁能告诉我在这里要做的事情。提前谢谢。

4

1 回答 1

4

Delete the line where you change Props.whereToLook. It should already be set to the right values. (more info on the Lift wiki)

A note: The contents of the src/main/resources folder will normally be added directly into your classpath at runtime. So the file src/main/resources/props/default.props should be available via a call to someClassInYourProject.getResourceAsStream("/props/default.props"). Don't try to refer to a file in your source directory at runtime, as it will only ever cause headaches later on.

于 2013-04-19T11:40:09.307 回答