开发者

Problem connecting to SQL Server database from Scala

I am trying to connect to a SQL Server database from my Scala code, but I'm getting a 'No suitable driver found' exception. My Scala code is as follows:

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


val conn = DriverManager.getConnection("jdbc:sqlserver://hostname:port/DBName?user=myusername&password=mypassword")
try {
    val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
    val rs = statement.executeQuery("select * from SomeTableName")
    while(rs.next()) {
        println(rs.getInt("CustomerID"))
    }

}
catch {
    case e => e.printStackTrace()
}
finally {
    conn.close()
}

I then ran the following command :

scala -cp /usr/lib/jtds-1.2.5.jar dbprog.scala

I have also tried using JDBC JARs for SQL Server provided by Microsoft sqljdbc.jar and sqljdbc4.jar.

And below is the stacktrace I get :

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://hostname:port/DBName?user=myusername&password=mypassword
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:207)
        at Main$$anon$1.<init>((virtual file):8)
        at Main$.main((virtual file):4)
        at Main.main((virtual file))
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.ObjectRunner$$anonfun$run$1.apply(ObjectRunner.scala:75)
        at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74)
        at scala.tools.nsc.ScriptRunner$.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:381)
        at scala.tools.nsc.ScriptRunner$$ano开发者_Python百科nfun$runScript$1.apply(ScriptRunner.scala:414)
        at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:413)
        at scala.tools.nsc.ScriptRunner$.withCompiledScript(ScriptRunner.scala:351)
        at scala.tools.nsc.ScriptRunner$.runScript(ScriptRunner.scala:413)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:168)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

What am I doing wrong and how can I fix that ?


Joe You need to do!

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

Class.forName("org.postgresql.Driver")
val conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/clients","username","password")

then Download -jar db if you need postgresql like me :-D wget postgresql-9.0-801.jdbc3.jar or ~mysql.jar

exec: scala -cp postgresql-9.0-801.jdbc3.jar file.scala


Try including this ahead of your getConnection (modify it to reflect SQLServer driver)

classOf[com.mysql.jdbc.Driver]

I wrote up a full example of using JDBC and Scala here http://solog.co/36/using-scala-with-jdbc-to-connect-to-mysql/


Replace

val conn = DriverManager.getConnection("jdbc:sqlserver://hostname:port/DBName?user=myusername&password=mypassword")

with

Class.forName("net.sourceforge.jtds.jdbcx.JtdsDataSource")
val conn = DriverManager.getConnection("jdbc:jtds:sqlserver://hostname:port/DBName","myusername","mypassword")


I found this piece of code to work for me:

Try registering your driver with the following:

java.sql.DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver)

rather than using classOf[net.sourceforge.jtds.jdbc.Driver] or Class.forName("net.sourceforge.jtds.jdbc.Driver"),

for example:

  val uri = "jdbc:jtds:sqlserver://localhost:1433/yourdatabae"
  val username = "username"
  val password = "password"

  DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver)

  private val conn: Connection = DriverManager.getConnection(uri, username, password)

  def getConn(): Connection = conn

  def main(args: Array[String]) = {

    val sql = "SELECT SOMETHING FROM YOURTABLE"
    val results = getConn.createStatement.executeQuery(sql)

    processResults(results)

    getConn.close

  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜