4
votes

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.

2
By chance have you also recently switched to using Java 9? - Gord Thompson
which compiler did your Intellij use? When I switched to Intellij, my project Language level is always set to 1.5. Open Module Settings and change to correct language level should help. - Haifeng Zhang
Yes I have the java 9 JDK and sqljdbc42 as module dependencies. - J.Johnson
@haifzhan Was on language level 9 and also tried language level 8. Neither are working still getting the same error Message. Here's a view of the project gyazo.com/17ff5d773ea99e79458393042c883f78 - J.Johnson
Do you use JDK 9 as Module/Project JDK? - y.bedrov

2 Answers

7
votes

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.

3
votes

You can get this to work if you include the extra jvm parameter

--add-modules=java.se.ee

Which in your run configuration in Intellij would go here

enter image description here

This then includes several modules which in java 9 have been marked as deprecated for future removal so these could be removed in java 10 so it is only really a temporary solution until Microsoft release java 9 compatible jdbc drivers.

More on this can be found in the Java9 Migration guide