我最近切换到intellij,但我发现很难连接到我的localDB.相同的代码适用于eclipse.此外,我已经添加了sqljdbc42.jar作为模块依赖项.package com.enter;import java.sql.*;public class SqlConnect {private String use...
我最近切换到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)方法和同样的错误.
解决方法:
正如微软在GitHub所述:
Currently none of our driver released Jars are compatible with JDK9.
您可以切换到使用Java 8 JDK,也可以将Microsoft的开发代码从他们的’JDBC4.3’分支合并到您的项目中,并将其与Java 9 JDK一起使用.
本文标题为:SQL Server JDBC的“NoClassDefFoundError:javax / xml / bind / DatatypeConverter”
基础教程推荐
- Java基于对象流实现银行系统 2023-05-19
- 基于Java实现收发电子邮件功能 2023-03-07
- Java基础01-JVM内存分析 2023-09-01
- java – 在mssql的情况下,metadata.getTables结果集为空 2023-11-04
- 在Java中轻松使用工厂设计模式介绍 2023-05-14
- SpringBoot集成Kafka 配置工具类的详细代码 2023-06-01
- Spring AOP详解面向切面编程思想 2023-01-18
- Spring Boot项目如何优雅实现Excel导入与导出功能 2022-12-27
- Spring实现泛型注入的示例详解 2023-02-20
- SpringMVC按Ctrl上传多个文件的方法 2023-05-14