2

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$$anonfun$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 ?

1
  • Have you tried manually registering the driver before getting the connection, using DriverManager.registerDriver(jtdsDriverInstance) Commented Jul 4, 2011 at 13:34

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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/

2 Comments

Its sqlserver not mysql that im trying to connect to. What should be the classOf statement in my case ?
It depends on which driver you use, for Microsoft's driver (sqljdbc) it would be: classOf[com.microsoft.sqlserver.jdbc.SQLServerDriver]
0

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")

2 Comments

While a great suggestion to the OP to use the superior jTDS driver, you failed to mention that he would have to download the driver and include in his application. If you modify your answer to include this I will upvote.
Just below his code snippet, the OP has indicated that he has tested it with jtds, so he has already downloaded the driver
0

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

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.