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

13
  • 1
    you need to add the driver lib to the classpath Commented Jun 13, 2020 at 3:53
  • @WSouBar the .jar for the driver is already added Commented Jun 13, 2020 at 3:55
  • what is the library that u r using and how have you added this jar in your classpath Commented Jun 13, 2020 at 3:58
  • @codeogeek on vscode we can add a external jar file from the "Java Dependencies" tab > Referenced Libraries Commented Jun 13, 2020 at 4:01
  • @codeogeek i'm using the default lib that microsoft published for sql server connection: learn.microsoft.com/pt-br/sql/connect/jdbc/… Commented Jun 13, 2020 at 4:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

damn, that was a simple issue, I changed the absolute path to my openjdk 11 on vscode and everything worked! thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.