I'm trying to create a convenience wrapper around my database calls in scala by using a partially applied function:
def queryResult[B](connection: Connection, sql: String)(process: (CallableStatement,ResultSet) => B): B =
using (connection) { connection =>
using (connection.prepareCall(sql)) { statement =>
var rs: ResultSet = null
try {
process(statement, rs)
} finally if (rs != null) {
try {
rs.close()
}
catch {
case e: SQLException => {}
}
}
}
}
Mainly for automatically closing connections/resultsets. However, I would like the rs
variable to return as a var, not a val. Whenever I try to assign rs to a resultset from a query, I get an error that I can't reassign to val.
Alternatively, if you have other ways to maximize code reuse for opening/closing streams/connections, please share too.
Edit: As Randall Schulz mentioned, Scala ARM would address the actual problem. The question as I posted it, however, is not supported in Scala.