Kenny_I
Kenny_I

Reputation: 2513

java.sql.SQLException: No suitable driver found for jdbc:sqlserver:// - Java based Azure Functions

I have java based Azure Functions with, which is writing to Azure SQL. However I get error when Function is triggered. What I need to do with VS Code for driver issue? I'm fine with JDBC or any other drive which is best with Azure Functions

https://learn.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://learn.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);
}

}

Upvotes: 1

Views: 626

Answers (1)

Tony Ju
Tony Ju

Reputation: 15619

What I need to do with VS Code for driver issue?

You need to add the jdbc driver dependency to the pom.xml file. Here is the dependency sample I used to connect to Azure SQL.

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

Reference:

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

Upvotes: 1

Related Questions