Created
August 31, 2016 06:20
-
-
Save RICH0423/20252ae06cf271cf6013a6458dff0783 to your computer and use it in GitHub Desktop.
Scala Connecting to MySQL with JDBC
This file contains hidden or 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.{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