Spark Shell写入mysql主要测试用

亲测有效
var cn: java.sql.Connection = null
var ps: java.sql.PreparedStatement = null
val sql = "insert into score007(score_total,score_behavior,score_history_credit,score_identity,
score_performance,score_social_cycle) values(?,?,?,?,?,?)"
val driver = "com.mysql.jdbc.Driver" 
Class.forName(driver)
try {
cn = java.sql.DriverManager.getConnection("jdbc:mysql://49.4.65.195:3306/mysql", "root", "root")
ps = cn.prepareStatement(sql)
for(j <- 1 to 5; i <- 1 to 6){
ps.setInt(i, scala.util.Random.nextInt(30))
if(i == 6){
ps.execute()
}
}
} catch {
case e: Exception => println("Mysql Exception")
} finally {
if (ps != null) {
ps.close()
}
if (cn != null) {
cn.close()
}
}


需要mysql-connector-java.jar在spark/jars目录下

你可能感兴趣的:(Spark Shell写入mysql主要测试用)