Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"NoClassDefFoundError: javax/xml/bind/DatatypeConverter" with SQL Server JDBC

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.

like image 588
J.Johnson Avatar asked Dec 23 '22 12:12

J.Johnson


1 Answers

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:

Currently none of our driver released Jars are compatible with JDK9.

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.

like image 79
Gord Thompson Avatar answered Dec 26 '22 12:12

Gord Thompson