Gabriel de Oliveira
Gabriel de Oliveira

Reputation: 65

Problems while trying to connect Java with SQL Server Express

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

Upvotes: 1

Views: 781

Answers (1)

Mark Rotteveel
Mark Rotteveel

Reputation: 108955

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.

Upvotes: 2

Related Questions