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