我正在尝试从我的Scala代码连接到SQL Server数据库,但是我找到了“找不到合适的驱动程序”异常。我的Scala代码如下:
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()
}
然后我运行了以下命令:
scala -cp /usr/lib/jtds-1.2.5.jar dbprog.scala
我还尝试过为Microsoft sqljdbc.jar
和sqljdbc4.jar
提供的SQL Server使用JDBC JAR。
以下是我得到的堆栈跟踪:
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)
我做错了什么,我该如何解决?
答案 0 :(得分:1)
尝试在getConnection之前包含此内容(修改它以反映SQLServer驱动程序)
classOf[com.mysql.jdbc.Driver]
我在这里写了一个使用JDBC和Scala的完整示例 http://solog.co/36/using-scala-with-jdbc-to-connect-to-mysql/
答案 1 :(得分:1)
乔 你需要做的!
import java.sql.{Connection, DriverManager, ResultSet}
Class.forName("org.postgresql.Driver")
val conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/clients","username","password")
然后下载-jar db
如果你需要像我这样的postgresql :-D wget postgresql-9.0-801.jdbc3.jar
或~mysql.jar
exec: scala -cp postgresql-9.0-801.jdbc3.jar file.scala
答案 2 :(得分:0)
替换
val conn = DriverManager.getConnection("jdbc:sqlserver://hostname:port/DBName?user=myusername&password=mypassword")
与
Class.forName("net.sourceforge.jtds.jdbcx.JtdsDataSource")
val conn = DriverManager.getConnection("jdbc:jtds:sqlserver://hostname:port/DBName","myusername","mypassword")
答案 3 :(得分:0)
我发现这段代码对我有用:
尝试使用以下内容注册您的驱动程序:
java.sql.DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver)
而不是使用
classOf[net.sourceforge.jtds.jdbc.Driver]
或Class.forName("net.sourceforge.jtds.jdbc.Driver")
,
例如:
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
}