0

I cannot seem to connect my JavaFX app to SQL Server due to a problem in the JDBC Driver.

I've added the .jar to the classpath in Project Structure -> Libraries.

public static void main(String[] args) {    
    String url = "jdbc:sqlserver://DANI-LAPTOP-W11\\SQLEXPRESS;databaseName=ABD_TRAB1";
    String user = "MYNAMEISHERE";
    String password = "MYPASSWORDISHERE";

    String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    try {
        Class.forName(driver).newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    try {
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("Connected to Microsoft SQL Server!");
    } catch (SQLException e) {
        System.out.println("Could not connect to database!");
        e.printStackTrace();
    }

    launch();
}`

The exception is thown when it gets to line 31 (Class.forName(driver).newInstance();):

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:375) at com.example.asbd_t1@1.0-SNAPSHOT/com.example.asbd_t1.HelloApplication.main(HelloApplication.java:31) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071) java.sql.SQLException: No suitable driver found for jdbc:sqlserver://DANI-LAPTOP-W11\SQLEXPRESS;databaseName=ABD_TRAB1 at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229) at com.example.asbd_t1@1.0-SNAPSHOT/com.example.asbd_t1.HelloApplication.main(HelloApplication.java:39) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)

SDK: 17

IDE: IntelliJ

Since the JDBC .jar is already added to the classpath, what is the problem?

daniela
  • 1
  • 1
  • 3
    The JDBC jar clearly isn't added to the classpath. Or it's not the MSSQL driver. Or it's some newer version whose name is no longer `com.microsoft.sqlserver.jdbc.SQLServerDriver`. Note that Class.forName is not neccessary (and hasn't been for 20 years). And `e.printStackTrace();` is really bad. The correct 'nevermind' content of a catch block is `throw new RuntimeException("uncaught", e);`. Update your IDE templates. – rzwitserloot Mar 27 '22 at 23:04
  • @rzwitserloot it is added tho. It's there, under libraries: https://imgur.com/a/MXKTx79 – daniela Mar 27 '22 at 23:43
  • 1
    The error trumps your screenshot. – rzwitserloot Mar 28 '22 at 01:33
  • @daniela How are you running the application through the IDE or creating a JAR and running the jar from terminal/cmd – seenukarthi Mar 28 '22 at 04:27
  • It is curious that all your other dependencies in that screenshot seem to be managed by Gradle, but this one isn't. – Mark Rotteveel Mar 28 '22 at 13:03
  • @MarkRotteveel I downloaded the .jar from the Microsoft website and added it to the libraries. I saw that approach used on several websites. I also tried creating a new projecto using maven instead of gradle, but non of the dependencies available match the correct version (17) – daniela Mar 28 '22 at 14:36
  • @KarthikeyanVaithilingam through the IDE – daniela Mar 28 '22 at 14:37
  • @rzwitserloot is there any other way I can add it to the class path? I’ve tried creating a new project and using maven instead but the driver’s version is an old one and it doesn’t match any of the SKD available (11 -> 17) – daniela Mar 28 '22 at 14:39
  • 1
    @daniela Why can't you use `implementation 'com.microsoft.sqlserver:mssql-jdbc:10.2.0.jre17'` in your build.gradle? In an case, even older driver version will likely work fine under Java 17. – Mark Rotteveel Mar 28 '22 at 16:57

0 Answers0