【发布时间】:2020-03-01 23:31:01
【问题描述】:
def jdbcReader(user: String, pwd: String, url: String, id: Int): List[mutable.Map[String, String] = {
var KeyVal = mutable.Map[String, String]()
var connection: Connection = null
try {
connection = DriverManager.getConnection(url, user, pwd)
val statement = connection.createStatement()
val resultSet = statement.executeQuery("SELECT Nam, Value FROM tablename WHERE id=?".replace("?", id.toString))
while (resultSet.next()) {
val name = resultSet.getString("Nam")
val value = resultSet.getString("Value")
// println("name , value = " + name + ", " + value)
}
} catch {
case e => e.printStackTrace()
val t = e.getMessage()
}
}
如何将元素添加到地图列表并从此方法返回?我相信你已经知道我是 Scala 的新手
【问题讨论】:
-
您能否准确说明您要添加到此列表中的内容?一张空地图?
-
试图将键值对字符串添加到列表映射
-
你有一个地图列表而不是列表地图。我当然也不明白您为什么要返回 Map 列表。一个简单的地图就足够了,不是吗?
-
var KeyVal =mutable.Map[String, String] var connection:Connection = null try { val name = resultSet.getString("Nam") val value = resultSet.getString("Value") KeyVal += (name -> value) } } catch { case e => e.printStackTrace() val t =e.getMessage() } }
-
当我添加一个元素 KeyVal += (name -> value) 时,它不断抛出类型不匹配
标签: scala jdbc mutablemap