用一个常用的例子


vim connMysql.scala

import java.sql.DriverManager
import java.sql.Connection
object ScalaJdbcConnectSelect {
def main(args: Array[String]) {
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://localhost/mysql"
val username = "root"
val password = "root"
var connection:Connection = null

try {
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password)
val statement = connection.createStatement()
val resultSet = statement.executeQuery("SELECT host, user FROM user")
while ( resultSet.next() ) {
val host = resultSet.getString("host")
val user = resultSet.getString("user")
println("host, user = " + host + ", " + user)
}
}
catch {
case e => e.printStackTrace
}
connection.close()
}
}

调用:

scala -cp /usr/local/spark/spark-1.3.0-bin-hadoop2.3/lib/mysql-connector-java-5.1.6-bin.jar:. connMysql.scala

如果没有-cp这个部分的话,mysql的jdbc驱动是没办法找到的。