5

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.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
J.Johnson
  • 89
  • 1
  • 1
  • 8
  • By chance have you also recently switched to using Java 9? – Gord Thompson Nov 21 '17 at 21:00
  • 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 Nov 21 '17 at 21:00
  • Yes I have the java 9 JDK and sqljdbc42 as module dependencies. – J.Johnson Nov 21 '17 at 21:16
  • @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 https://gyazo.com/17ff5d773ea99e79458393042c883f78 – J.Johnson Nov 21 '17 at 21:25
  • Do you use JDK 9 as Module/Project JDK? – y.bedrov Nov 22 '17 at 10:15
  • Related: https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j – Mark Rotteveel Feb 13 '18 at 08:58

3 Answers3

7

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Thank you so much. Java 9 JDK doesn't work regardless (of changing language mode down to 8) Switched to java 8 JDK worked no questions asked. – J.Johnson Nov 22 '17 at 18:50
  • Thanks. Gord. How can I make MS's development code at https://github.com/Microsoft/mssql-jdbc/tree/JDBC4.3?files=1 into a .jar file? –  Dec 05 '17 at 22:39
  • @Ethan - There are jars available from a post to the GitHub issue [here](https://github.com/Microsoft/mssql-jdbc/issues/549#issuecomment-348312186). They also mention that we should be able to build the jars using Maven (`mvn install`). – Gord Thompson Dec 05 '17 at 22:45
  • 1
    @GordThompson I updated your answer to include that a Java 9+ version has been made available. I hope you don't mind. – Mark Rotteveel Jan 17 '20 at 08:17
3

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

Java Devil
  • 10,629
  • 7
  • 33
  • 48
2

If you have a maven build, add the following to your pom.xml

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>
Anurag Bhalekar
  • 830
  • 7
  • 9