1

So, I'm making a simple app where my code connect to a SQL Server Express database. I've all configurated, JDBC, a database, a logon and password. But I keep getting the same error when I try to run the code. The error was that:

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=SampleDatabase;user=testLogon;password=sample123
        at java.sql.DriverManager.getConnection(DriverManager.java:689)
        at java.sql.DriverManager.getConnection(DriverManager.java:270)
        at App.main(App.java:10)

I'm currently using VSCode for development, but I have already changed to IntelliJ and Eclipse and I keep getting the same error.

My code:

import java.sql.*;

public class App {
    public static void main(String[] args) throws Exception {
        String connectionUrl = "jdbc:sqlserver://localhost;databaseName=SampleDatabase;user=testLogon;password=sample123";
        String insertString = "INSERT INTO Pessoa (id, nome, idade) VALUES (?, ?, ?)";

        try (
            Connection con = DriverManager.getConnection(connectionUrl);
            PreparedStatement stmt = con.prepareStatement(insertString);
        ) {
            Pessoa p1 = new Pessoa(1, "Maria", 50);

            stmt.setInt(1, p1.getId());
            stmt.setString(2, p1.getNome());
            stmt.setInt(3, p1.getIdade());

            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

JDBC jar is already imported

enter image description here

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    you need to add the driver lib to the classpath – WSouBar Jun 13 '20 at 03:53
  • @WSouBar the .jar for the driver is already added – Gabriel de Oliveira Jun 13 '20 at 03:55
  • what is the library that u r using and how have you added this jar in your classpath – codeogeek Jun 13 '20 at 03:58
  • @codeogeek on vscode we can add a external jar file from the "Java Dependencies" tab > Referenced Libraries – Gabriel de Oliveira Jun 13 '20 at 04:01
  • @codeogeek i'm using the default lib that microsoft published for sql server connection: https://learn.microsoft.com/pt-br/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15 – Gabriel de Oliveira Jun 13 '20 at 04:02
  • Since you have mentioned SQL Server express, try this - String dbURL = "jdbc:sqlserver://localhost\\sqlexpress;user=sa;password=secret"; Connection conn = DriverManager.getConnection(dbURL); if (conn != null) { System.out.println("Connected"); } – codeogeek Jun 13 '20 at 04:08
  • @codeogeek now I get this error: Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost\sqlexpress;user=testLogon;password=sample123 – Gabriel de Oliveira Jun 13 '20 at 04:12
  • Got it. Try adding the below in your .vscode/settings.json file (if not present, you can create one) { "java.project.referencedLibraries": [ "lib/**/*.jar", "c:\\path\\to\\jarfile\\mssql-jdbc-8.x.x.jre11.jar" ] } Mention your correct path Refer the answer here - https://stackoverflow.com/questions/50232557/visual-studio-code-java-extension-howto-add-jar-to-classpath – codeogeek Jun 13 '20 at 04:18
  • @codeogeek keeps getting the same error, my settings: { "java.project.referencedLibraries": [ "lib/**/*.jar", "lib/mssql-jdbc-8.2.2.jre11.jar" ] } – Gabriel de Oliveira Jun 13 '20 at 04:22
  • Can you add the absolute path of the jar – codeogeek Jun 13 '20 at 04:36
  • @codeogeek the error persists, updated settings: { "java.project.referencedLibraries": [ "lib/**/*.jar", "C:\\Users\\biewo\\Documents\\Faculdade\\Database\\SQL\\lib\\mssql-jdbc-8.2.2.jre11.jar" ] } – Gabriel de Oliveira Jun 13 '20 at 04:39
  • Ok 1. try restarting the vsstudio code and try again Last option which I see right now is running your main class from terminal while giving classpath using flag -cp – codeogeek Jun 13 '20 at 04:47
  • @codeogeek tried, but the error continues. thx for the help but apparently it will be very complicated to fix, I'll leave it for now – Gabriel de Oliveira Jun 13 '20 at 05:00

1 Answers1

2

The problem is that you are using Java 8 (Java 8 update 241) to run your program, but are trying to use the version of the Microsoft SQL Server JDBC driver for Java 11 and higher (as indicated by the jre11 in the version).

Given the driver is compiled for Java 11, it cannot be loaded by Java 8. Download the Java 8 version of the driver (ending in jre8), or upgrade to Java 11.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197