0

I'm trying to write Function, which get triggered by Service Bus and write code to Azure. I'm here stuck not able to access DB at all. I don't get any error here, but neither I get Print results from select statement(Test2).

What is wrong in code?

Before running this code, I have downloaded jdbc driver and followed following example. http://biercoff.com/add-microsoft-sql-jdbc-driver-to-maven/

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:
        */


            String connectionUrl = 
"jdbc:sqlserver://sql...;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";

            ResultSet resultSet = null;

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

                    context.getLogger().info("Test 1"); // This is printed out.

                // 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));
                    context.getLogger().info("Test 2"); // This is never printed.

                }
        }
            // Handle any errors that may have occurred.
            catch (SQLException e) {
                e.printStackTrace();
            }

        context.getLogger().info("Message: " + message); // this is printed.
    }
}
0

1 Answer 1

1

I have tested the sql part by using your code, the code works fine.

enter image description here

As you didn't got any error, the only reason that I can think of is that there is no data in the table(RadioEventsTarget).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.