Skip to content

Instantly share code, notes, and snippets.

@RICH0423
Created August 31, 2016 06:20
Show Gist options
  • Select an option

  • Save RICH0423/20252ae06cf271cf6013a6458dff0783 to your computer and use it in GitHub Desktop.

Select an option

Save RICH0423/20252ae06cf271cf6013a6458dff0783 to your computer and use it in GitHub Desktop.
Scala Connecting to MySQL with JDBC
import java.sql.{Connection,DriverManager}
import com.typesafe.scalalogging._
import org.slf4j.LoggerFactory
object MysqlJDBC extends App {
val logger = Logger(LoggerFactory.getLogger(this.getClass))
val url = "jdbc:mysql://10.120.136.90:3306/hackpad"
val driver = "com.mysql.jdbc.Driver"
val username = "root"
val password = "ssir"
var connection: Connection = _
try {
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password)
val statement = connection.createStatement
val rs = statement.executeQuery("SELECT email, token FROM email_signup")
while (rs.next) {
val email = rs.getString("email")
val token = rs.getString("token")
logger.info("Email: {}, Token: {}", email, token)
}
} catch {
case e: Exception => logger.error("JDBC error", e)
}
connection.close
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment