我最近切换到intellij,但我发现很难连接到我的localDB。相同的代码适用于eclipse。 此外,我已将sqljdbc42.jar添加为模块依赖项。
package com.enter;
import java.sql.*;
public class SqlConnect {
private String username, password, url;
public Connection conn;
public SqlConnect() {
username = "user=admin;";
password = "password=admin";
url = "jdbc:sqlserver://Bossman-PC\\SQL2014TRAINING;databaseName=EnterDB;";
Connect();
}
public SqlConnect(String user, String pass) {
username = user;
password = pass;
url = "jdbc:sqlserver://Bossman-PC\\SQL2014TRAINING;databaseName=EnterDB;";
Connect();
}
public void Connect() { //Loads sql driver and creates a connection object with local database
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = url + username + password;
conn = DriverManager.getConnection(connectionUrl);
System.out.println("Connected.");
} catch(Exception e) {
e.printStackTrace();
}
}
public Connection getConnection() {
return conn;
}
}
产生错误:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4098)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3160)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3123)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1981)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:678)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)
at com.enter.SqlConnect.Connect(SqlConnect.java:25)
at com.enter.SqlConnect.<init>(SqlConnect.java:12)
at com.enter.Login.makeConnection(Login.java:26)
at com.enter.Login.<init>(Login.java:16)
at com.enter.Execute.initLogin(Execute.java:14)
at com.enter.Execute.main(Execute.java:9)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 19 more
Process finished with exit code 1
非常感谢任何帮助。我也尝试过重载的DriverManager.getConnection(url,user,pass)方法和同样的错误。
答案 0 :(得分:4)
微软在GitHub上指出:
目前我们的驱动程序发布的Jars都没有与JDK9兼容。
您可以切换到使用Java 8 JDK,也可以使用他们的&#39; JDBC4.3&#39;中的Microsoft开发代码。分支到您的项目中并将其与Java 9 JDK一起使用。
答案 1 :(得分:3)
如果包含额外的jvm参数
,则可以使其正常工作--add-modules=java.se.ee
Intellij中的运行配置中的哪一个会在这里
这包括几个模块,这些模块在java 9中已被标记为已弃用,以便将来删除,因此这些模块可以在java 10中删除,因此它只是一个临时解决方案,直到Microsoft发布java 9兼容的jdbc驱动程序。
有关详情,请参阅Java9 Migration guide