简体   繁体   中英

Connection with SQL Server: java.sql.SQLException: No suitable driver found for jdbc:sqlserver

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:

在此处输入图像描述

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

另一台机器上的工作案例

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM