Skip to content

Instantly share code, notes, and snippets.

@makisarvin
Created September 26, 2013 19:49
Show Gist options
  • Save makisarvin/6719586 to your computer and use it in GitHub Desktop.
Save makisarvin/6719586 to your computer and use it in GitHub Desktop.
Code Sample for using JDBC with scala. The gem is the translation of the ResultSet to a stream (http://stackoverflow.com/questions/9636545/treating-an-sql-resultset-like-a-scala-stream)
object DaoUtil {
import java.sql.DriverManager
def main(args:Array[String]) {
Class.forName("com.mysql.jdbc.Driver").newInstance
val con = DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=root&password=password")
val stmt = con.createStatement()
val rs = stmt.executeQuery("Select count(*) from samples")
val rsStream = new Iterator[String] {
def hasNext = rs.next()
def next() = rs.getString(1)
}.toStream
rsStream.foreach(println)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment