Scala的JDBC操作MySQL

package com.ruozedata.bigdata.scala03

import java.sql.DriverManager

object ScalaJDBCApp {
  def main(args: Array[String]): Unit = {
    val url = "jdbc:mysql://hadoop000:3306/ruoze_d6"
    val user = "root"
    val password = "root"
    val sql = "select DB_ID,DB_LOCATION_URI,NAME FROM DBS"

    // Class.forName("com.mysql.jdbc.Driver")
//    classOf[com.mysql.jdbc.Driver]

    val connection = DriverManager.getConnection(url,user,password)
    val stmt = connection.createStatement()
    val rs = stmt.executeQuery(sql)
    while(rs.next()) {
      val dbId = rs.getLong(1)
      val location = rs.getString(2)
      val name = rs.getString(3)
      println(dbId + " ... " + location + " ... " + name)
    }
    rs.close()
    stmt.close()
    connection.close()
  }

}

你可能感兴趣的:(Scala的JDBC操作MySQL)