gpt4 book ai didi

java - 如何连接到local.settings.json的JDBC连接字符串?

转载 作者:行者123 更新时间:2023-12-04 10:12:12 24 4
gpt4 key购买 nike

我想通过基于 Java 的 Azure 函数写入 SQL 数据库。

我在 local.settings.json 中有 JDBC 的 SQLconnstring。如何从设置文件中正确获取此连接而不是硬编码到 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);
}
}

最佳答案

您可以使用 System.getenv("")local.settings.json 获取值文件。例如,我们可以使用 String connectionUrl =System.getenv("SQLConnectionString");在 local.settings.json 文件中获取 sql 连接字符串。

enter image description here

关于java - 如何连接到local.settings.json的JDBC连接字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61271452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com