如何连接到local.settings.json的JDBC连接字符串? [英] How to connect to JDBC connecting string of local.settings.json?
问题描述
我想通过基于Java的Azure Functions写入SQL数据库.
I would like to write to SQL database by Java based Azure Functions.
我在local.settings.json中有JDBC的SQLconnstring.如何从设置文件正确地建立此连接,而不是硬编码到Java文件?
I have SQLconnstring of JDBC in local.settings.json. How to get this connection correctly from settings file instead of hard coding to java file?
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连接字符串.
You can use System.getenv("")
to get the value from local.settings.json
file. For example, we can use String connectionUrl =System.getenv("SQLConnectionString");
to get the sql connection string in the local.settings.json file.
这篇关于如何连接到local.settings.json的JDBC连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!