java.sql.SQLException:连接到Azure数据库时找不到适用于jdbc:sqlserver的驱动程序

问题描述 投票:0回答:1

im使用azure数据库,然后将所有必需的JAR文件添加到我的库中。这是我的代码,我不确定为什么它无法连接。请指教。

 String connectionUrl =
                "jdbc:sqlserver://bcs430-final-project.database.windows.net:1433;"
                        + "database=OASIS ASSISTANT;"
                        + "user=farmingdale@bcs430-final-project;"
                        + "password=bcs430w!;"
                        + "encrypt=true;"
                        + "trustServerCertificate=false;"
                        + "loginTimeout=30;";




     String insertSql = "select * from dbo.BCS102 where crse = bcs102 ";

        ResultSet resultSet = null;

        try (Connection connection = DriverManager.getConnection(connectionUrl);
                PreparedStatement prepsInsertProduct = connection.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);) {

            prepsInsertProduct.execute();
            // Retrieve the generated key from the insert.
            resultSet = prepsInsertProduct.getGeneratedKeys();

            // Print the ID of the inserted row.
            while (resultSet.next()) {
                System.out.println("Generated: " + resultSet.getString(1));
            }
        }
        // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
        }

这里是错误

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://bcs430-final-project.database.windows.net:1433;database=OASIS ASSISTANT;user=farmingdale@bcs430-final-project;password=bcs430w!;encrypt=true;trustServerCertificate=false;loginTimeout=30;
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at SeniorProject.main(SeniorProject.java:35)
azure-sql-database sqlexception
1个回答
0
投票

错误表明您没有为JDBC安装合适的驱动器。

请检查是否已添加依赖项。使用以下代码将Microsoft SQL Server JDBC驱动程序添加到项目的依赖项中。

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>7.0.0.jre8</version>
</dependency>

有关更多详细信息,您可以参考本文档Quickstart: Use Java to connect to and query an Azure SQL database。可以按照本教程进行操作,避免出现此错误。

此外,您想通过AD身份验证连接到Azure SQL数据库,您的连接字符串应如下所示:

jdbc:sqlserver://***.database.windows.net:1433;database=Mydatabase;user={your_username_here};password={your_password_here};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;authentication=ActiveDirectoryPassword

您可以在Portal上获得AD连接字符串:

enter image description here

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.