数据库
首页 > 数据库> > SQL Server JDBC的“NoClassDefFoundError:javax / xml / bind / DatatypeConverter”

SQL Server JDBC的“NoClassDefFoundError:javax / xml / bind / DatatypeConverter”

作者:互联网

我最近切换到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一起使用.

标签:java,sql,intellij-idea,jdbc,mssql-jdbc
来源: https://codeday.me/bug/20190611/1216619.html