简体   繁体   中英

how to create a connection between scala and mysql using jdbc

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
object TestAppMain {

  def main(args: Array[String]) {

    val url = "jdbc:mysql://localhost/scala"
    val username = "root"
    val password = "abc123"

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

    try {
      // make the connection
      **Class.forName("com.mysql.jdbc.Driver")**
      connection = DriverManager.getConnection(url, username, password)

      // create the statement, and run the select query
      val statement = connection.createStatement()
      val resultSet = statement.executeQuery("SELECT host, user FROM fra1")
      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()**
  }
}

i am getting the error in the lines that are surrounded by "** **" but i am getting the main error in "com.mysql.jdbc.Driver" so how to resolve the error? i have the database named as scala and a table named as fra1 and i have created a database named as scala and created a table also in mysql

mysql> create table fra1(host varchar(64),user varchar(64));
Query OK, 0 rows affected (0.39 sec)

mysql> select* from fra1;
Empty set (0.00 sec)

mysql> insert into fra1 values('rohit1' ,'xyz123');
Query OK, 1 row affected (0.05 sec)

mysql> insert into fra1 values('rohit' ,'abc123');
Query OK, 1 row affected (0.05 sec)

mysql> select* from fra1;
+--------+--------+
| host   | user   |
+--------+--------+
| rohit1 | xyz123 |
| rohit  | abc123 |
+--------+--------+
2 rows in set (0.00 sec)

In jdbc 4.0, you don't need to do Class.forName any more. Check the jdbc manual here:

http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

Also, make sure you declare the right dependency in your Build.scala or build.sbt:

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

There are three steps to do it. 1. add libs in build.sbt

libraryDependencies ++= Seq (
  "mysql" % "mysql-connector-java" % "5.1.12"
)

2. import related libs

import java.util. Properties
import java.sql.{Connection, DriverManager, Statement, ResultSet}

3. scala code to connect with mysql

val url = "jdbc:mysql://127.0.0.1/clothing"
val username = "root"
val password = "123456"

Class.forName("com.mysql.jdbc.Driver")
val dbc: Connection = DriverManager.getConnection(url, username, password)
val st: Statement = dbc.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
val rs = st.executeQuery("SELECT * from movies LIMIT 10")
val result = new StringBuilder
while (rs.next()) {
  val info = Array(rs.getString("MovieID"), rs.getString("MovieName"), rs.getString("ReleaseYear"))
  result.append(rs.getString("MovieID")).append(",")
  result.append(rs.getString("MovieName")).append(",")
  result.append(rs.getString("ReleaseYear")).append("\n")
}
dbc.close
result.toString

this code works for me

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

object ConnectDB {
  var connection:Connection = _

  def main(args: Array[String]) {

    val url = "jdbc:mysql://localhost/mysql"
    val driver = "com.mysql.jdbc.Driver"
    val username = "root"
    val password = "root"


    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 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: Exception => e.printStackTrace
    }
    connection.close()
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM