Created
September 26, 2013 19:49
-
-
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)
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
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