0

我需要在我的 selenium 测试中连接到 SQL Server,我使用 jtds 1.3 驱动程序,我将它添加到我的项目的 Buildpath 中。

这是连接代码:

Class.forName("net.sourceforge.jtds.jdbc.Drive");
String url = "jdbc:jtds:sqlserver:localhost:1433/db_name;instance=SQLEXPRESS;"; 
final Connection con = DriverManager.getConnection(url, "login", "password");

但后来我运行我的项目,得到错误

java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Drive
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.SQLConnection.connectToSQL(SQLConnection.java:16)
at com.SqlRun.insert(SqlRun.java:58)
at com.SqlRun.insert(SqlRun.java:80)
at com.test.SQLRequest.checkTestPreconditions(SQLRequest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1203)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.run(TestNG.java:1036)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
4

1 回答 1

1

接口的 jTDS 实现java.sql.DriverDriver,不是Drive

更改Class#forName方法参数应该可以解决问题:

Class.forName("net.sourceforge.jtds.jdbc.Driver");
于 2013-02-13T14:43:18.357 回答