首页 > 解决方案 > java.sql.SQLException:找不到适合 jdbc:sqlserver:// - 基于 Java 的 Azure Functions 的驱动程序

问题描述

我有基于 java 的 Azure Functions,它正在写入 Azure SQL。但是,触发 Function 时出现错误。对于驱动程序问题,我需要用 VS Code 做什么?我可以使用 JDBC 或任何其他最适合 Azure Functions 的驱动器

https://docs.microsoft.com/en-us/sql/connect/jdbc/step-3-proof-of-concept-connecting-to-sql-using-java?view=sql-server-ver15

ERROR:

17.4.2020 13.06.39] java.sql.SQLException: No suitable driver found for jdbc:sqlserver://sql...;
[17.4.2020 13.06.39] Function "TopicTriggerSQLOutput" (Id: 70a1ce4c-3828-4280-81cf-dafa61956cb5) 
invoked by Java Worker
[17.4.2020 13.06.39]    at java.sql.DriverManager.getConnection(DriverManager.java:689)

CODE:

package com.function;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

/**
 * Azure Functions with Azure Storage Queue trigger.
 */
public class TopicTriggerSQLOutput {
     /**
     * This function will be invoked when a new message is received at the specified path. The 
message contents are provided as input to this function.
     */
     @FunctionName("TopicTriggerSQLOutput")
     public void run(
         @ServiceBusTopicTrigger(
            name = "message",
            topicName = "newtopic",
            subscriptionName = "newsubscription",
            connection = "topicconnstring"
        ) String message,
         final ExecutionContext context
    ) {
         /*Creating SQL Connection. I need help here:
         https://docs.microsoft.com/en-us/sql/connect/jdbc/step-3-proof-of-concept-connecting-to-sql- 
   using-java?view=sql-server-ver15
        */

            String connectionUrl = "jdbc:sqlserver://sql...";

            ResultSet resultSet = null;

            try (Connection connection = DriverManager.getConnection(connectionUrl);
                Statement statement = connection.createStatement();) {

               // Create and execute a SELECT SQL statement.
                String selectSql = "SELECT TOP 10 artist FROM [dbo].[RadioEventsTarget]";
                resultSet = statement.executeQuery(selectSql);

                // Print results from select statement
                while (resultSet.next()) {
                System.out.println(resultSet.getString(2) + " " + resultSet.getString(3));
                }
       }
            // Handle any errors that may have occurred.
            catch (SQLException e) {
            e.printStackTrace();
            }

    //context.getLogger().info(message);
}

}

标签: javaazure-functions

解决方案


对于驱动程序问题,我需要用 VS Code 做什么?

您需要将 jdbc 驱动程序依赖项添加到pom.xml文件中。这是我用来连接到 Azure SQL 的依赖项示例。

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

参考:

https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15


推荐阅读