本文介绍了“NoClassDefFoundError: javax/xml/bind/DatatypeConverter"使用 SQL Server JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近切换到 intellij,但我发现很难连接到我的 localDB.相同的代码在 Eclipse 上工作得很好.此外,我已经将 sqljdbc42.jar 添加为模块依赖项.

package com.enter;导入 java.sql.*;公共类 SqlConnect {private String 用户名、密码、url;公共连接连接;公共 SqlConnect() {用户名 = "用户 = 管理员;";密码 = "密码=管理员";url = "jdbc:sqlserver://Bossman-PC\SQL2014TRAINING;databaseName=EnterDB;";连接();}公共 SqlConnect(字符串用户,字符串传递){用户名 = 用户;密码 = 通行证;url = "jdbc:sqlserver://Bossman-PC\SQL2014TRAINING;databaseName=EnterDB;";连接();}public void Connect() {//加载sql驱动并创建与本地数据库的连接对象尝试 {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");字符串 connectionUrl = url + 用户名 + 密码;conn = DriverManager.getConnection(connectionUrl);System.out.println("已连接.");} 捕获(异常 e){e.printStackTrace();}}公共连接 getConnection() {返回连接;}}

产生的错误:

线程main"中的异常 java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter在 com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4098)在 com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3160)在 com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)在 com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3123)在 com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505)在 com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445)在 com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1981)在 com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)在 com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)在 com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)在 com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)在 java.sql/java.sql.DriverManager.getConnection(DriverManager.java:678)在 java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)在 com.enter.SqlConnect.Connect(SqlConnect.java:25)在 com.enter.SqlConnect.(SqlConnect.java:12)在 com.enter.Login.makeConnection(Login.java:26)在 com.enter.Login.(Login.java:16)在 com.enter.Execute.initLogin(Execute.java:14)在 com.enter.Execute.main(Execute.java:9)引起:java.lang.ClassNotFoundException:javax.xml.bind.DatatypeConverter在 java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)在 java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)在 java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)... 19 更多进程以退出代码 1 结束

任何帮助将不胜感激.我也试过重载的 DriverManager.getConnection(url, user, pass) 方法和同样的错误.

解决方案

对于 Java 9+ 兼容性,您需要使用 6.4.0 或更高版本作为您使用的由 jreXX 版本后缀标识的 Java 版本.使用低于或等于您 Java 版本的最高版本).

上一个回答

正如微软在 GitHub 上所述:>

目前我们发布的驱动程序中没有一个与 JDK9 兼容.

您可以切换到使用 Java 8 JDK,也可以将 Microsoft 的JDBC4.3"分支中的开发代码合并到您的项目中,并与 Java 9 JDK 一起使用.

I recently switched to intellij but I'm finding it hard to connect to my localDB. The same code worked on eclipse fine. Also I have already added the sqljdbc42.jar as a module dependency.

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;
    }


}

Error produced:

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

Any help would be greatly appreciated. I've also tried the overloaded DriverManager.getConnection(url, user, pass) method and same error.

解决方案

For Java 9+ compatibility, you need to use version 6.4.0 or higher for the Java version you use as identified by the jreXX version suffix. Use the highest version lower or equal to you Java version).


Previous answer

As noted by Microsoft on GitHub:

You can either switch to using the Java 8 JDK, or you can incorporate Microsoft's development code from their 'JDBC4.3' branch into your project and use that with the Java 9 JDK.

这篇关于“NoClassDefFoundError: javax/xml/bind/DatatypeConverter"使用 SQL Server JDBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 03:14