Last active
March 18, 2017 17:16
-
-
Save number23/74b1c017c3d0398454e4b5f530bffcaa to your computer and use it in GitHub Desktop.
java.sql.Result Iterator on Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql._ | |
def resultSetItr[T](res: ResultSet)(f: ResultSet => T): Stream[T] = { | |
new Iterator[T] { | |
def hasNext = res.next() | |
def next() = f(res) | |
}.toStream | |
} | |
def query[T](conn: Connection, sql: String)(f: ResultSet => T): List[T] = { | |
val s = conn.createStatement | |
try { | |
val res = s.executeQuery(sql) | |
val it = resultSetItr(res)(f) | |
it.toList | |
} finally { | |
s.close | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment