Home > Net >  SOLVED Connection with SQL Server: java.sql.SQLException: No suitable driver found for jdbc:sqlserve
SOLVED Connection with SQL Server: java.sql.SQLException: No suitable driver found for jdbc:sqlserve

Time:04-20

I am trying to connect a little Java App to a SQL Server database using Visual Studio Code. This is my code:

import java.sql.*;
public class App {
    public static void main(String[] args) {
        String connectionUrl =
                "jdbc:sqlserver://DESKTOP-03898L2DB;"
                          "Database=DB_PA;"
                          "User=checkou-GN;"
                          "Password=sapassword;"
                          "IntegratedSecurity=true;"
                          "encrypt=true;trustServerCertificate=true";
            ;
            try {
                //String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                //Class.forName(driver);
                try (Connection conn = DriverManager.getConnection(connectionUrl);) {
                
                System.out.println("Connected");
                }
            } catch (SQLException e) {
                System.out.println(e);
                e.printStackTrace();
            }
    }
}

But when I run it I get this message:

cd "c:\Users\checki\Desktop\JAVA\pruebaN3\prueba\src\" && javac App.java && java App
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://DESKTOP-033L2DB;Database=DB_PA;User=checkou-GN;Password=sapassword;IntegratedSecurity=true;encrypt=true;trustServerCertificate=true
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)
    at App.main(App.java:16)

I've succesfully imported the connectors and the SDK as shown in the picture:

enter image description here

The thing is that I've tried to do the same on another computer and it seems to work fine:

Working case on another machine

The main difference for me is the command that the shell is executing: in the first case is using javac and java and in the working case is using:

& 'C:\Program Files\Java\jdk-18\bin\java.exe' '--enable-preview' '-XX: ShowCodeDetailsInExceptionMessages' '@C:\Users\sempr\AppData\Local\Temp\cp_9hxr1t8qo8sc8hiaiyq1qpyf9.argfile' 'App'

I don't know what the problem could be and I've tried almost everything. In Eclipse and Intelij the program works fine on both machines but with VSCode in the first machine does not work.

Thanks for your help in advance.

CodePudding user response:

You are using Code Runner for the first case? Please choose Run Java button instead of Run Code button. The Code Runner will not find the referenced libraries.

  • Related