java - 如何连接到local.settings.json的JDBC连接字符串?
问题描述
我想通过基于 Java 的 Azure Functions 写入 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);
}
}
解决方案
推荐阅读
- nestjs - 注册其接口具有强制构造函数参数的提供者
- r - Dygraph 和 Graph 未在闪亮的选项卡下呈现
- c - 在 C 中计算二进制文件的排序,仅包含 uint16_t 数字
- javascript - javascript读取docx文本的问题
- swift - SCNSceneRendererDelegate - 未调用渲染器
- android - 在 ViewPager2 中启动子片段
- php - 实现千 (k, kk) 解释器
- python - 每个国家/地区的数据框中的 Python 总和值并保留 PopData
- angular - get方法以角度返回空对象
- ffmpeg - ffmpeg 转换为 hls 时烧录的字幕不同步