1

scala 代码

import java.sql.{DriverManager, Connection, ResultSet}
object hello {

  val user="root"
  val password = "password"
  val host="yourip"
  val database="yourdb"
  val conn_str = "jdbc:mysql://"+host +":3306/"+database+"?user="+user+"&password=" + password
  println(conn_str)


  def main(args:Array[String]): Unit ={
    //classOf[com.mysql.jdbc.Driver]
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    val conn = DriverManager.getConnection(conn_str)
    println("hello")
    try {
      // Configure to be Read Only
      val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)

      // Execute Query
      val rs = statement.executeQuery("SHOW TABLES")

      // Iterate Over ResultSet
      while (rs.next) {
        println(rs.getRow())
      }
    }
    catch {
      case _ : Exception => println("===>")
    }
    finally {
      conn.close
    }
  }
}

sbt代码

```name := "test"

version := "1.0"

organization :="com.timger.info"

version :="0.0.1-SNAPSHOT"

exportJars := true

libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.33"

resolvers += "Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/.m2/repository"


### 执行

sbt run
```


timger
631 声望22 粉丝

引用和评论

0 条评论