码迷,mamicode.com
首页 > 数据库 > 详细

sparksql链接mysql

时间:2015-08-19 10:46:20      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

1.在IDEA上建立一个sparksql_mysql的scala对象。

2.连接mysql的代码如下

import java.sql.{DriverManager, PreparedStatement, Connection}
import org.apache.spark.rdd.JdbcRDD
import org.apache.spark.{SparkContext, SparkConf}

object SparkSql_mysql {
  def main(args: Array[String]) {
    // connect to the database named "mysql" on the localhost
    val driver = "com.mysql.jdbc.Driver"
    val url = "jdbc:mysql://localhost:3306/test"
    val username = "root"
    val password = ""

    // there‘s probably a better way to do this
    var connection:Connection = null

    try {
      // make the connection
      Class.forName(driver)
      connection = DriverManager.getConnection(url, username, password)


      // create the statement, and run the select query
      val statement = connection.createStatement()

      val inserSql="insert into person(person_id,first_name,last_name,gender) values(6,‘yuefei‘,‘zhang‘,‘w‘);"
      statement.executeUpdate(inserSql)


      val resultSet = statement.executeQuery("SELECT first_name,last_name FROM person")
      while ( resultSet.next() ) {
        val first_name = resultSet.getString("first_name")
        val last_name = resultSet.getString("last_name")
        println("first_name, last_name = " + first_name + ", " + last_name)
      }

      println("resultSet",resultSet)

    } catch {
//      case e => e.printStackTrace
      case _: Throwable =>
        println("error come in ......")
    }
    connection.close()
  }
}

3.说明:

    val driver = "com.mysql.jdbc.Driver"   //指定mysql的dirver
    val url = "jdbc:mysql://localhost:3306/test" //指定mysql的地址,test为数据库
    val username = "root" //指定mysql的用户名
    val password = ""    //密码

sparksql链接mysql

标签:

原文地址:http://www.cnblogs.com/aijianiula/p/4741320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!