Как подключиться к JDB C соединительной строке local.settings. json? - PullRequest
2 голосов
/ 17 апреля 2020

Я хотел бы записать в SQL базу данных с помощью Java функций Azure.

У меня есть SQLconnstring JDB C в local.settings. json. Как правильно получить это соединение из файла настроек вместо жесткого кодирования в файл java?

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

            /*How to get connection string from local.settings.json instead of hard coding?*/
            String connectionUrl =
                    "jdbc:sqlserver://yourserver.database.windows.net:1433;"
                            + "database=AdventureWorks;"
                            + "user=yourusername@yourserver;"
                            + "password=yourpassword;"
                            + "encrypt=true;"
                            + "trustServerCertificate=false;"
                            + "loginTimeout=30;";

            try (Connection connection = DriverManager.getConnection(connectionUrl);) {
                // SQL Code here.
            }
            // Handle any errors that may have occurred.
            catch (SQLException e) {
                e.printStackTrace();
            }

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

1 Ответ

1 голос
/ 20 апреля 2020

Вы можете использовать System.getenv(""), чтобы получить значение из файла local.settings.json. Например, мы можем использовать String connectionUrl =System.getenv("SQLConnectionString"); для получения строки соединения sql в файле local.settings. json.

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...