February 2017
Beginner to intermediate
1100 pages
25h 19m
English
The JDBC
ResultSet object plays very badly with Scala collections. The only real way of doing anything useful with it is to loop through it directly with a while loop. For instance, to get a list of the names of physicists in our database, we could write the following code:
// WARNING: poor Scala code
import Implicits._ // import implicit conversions
SqlUtils.usingConnection("test") { connection =>
connection.withQuery("SELECT * FROM physicists") { resultSet =>
var names = List.empty[String]
while(resultSet.next) {
val name = resultSet.getString("name")
names = name :: names
}
names
}
}
//=> List[String] = List(Paul Dirac, Albert Einstein, Marie Curie, Richard Feynman, Isaac Newton)The ResultSet interface feels ...
Read now
Unlock full access