java - java.sql.SQLException:找不到适合 jdbc:sqlserver:// - 基于 Java 的 Azure Functions 的驱动程序
问题描述
我有基于 java 的 Azure Functions,它正在写入 Azure SQL。但是,触发 Function 时出现错误。对于驱动程序问题,我需要用 VS Code 做什么?我可以使用 JDBC 或任何其他最适合 Azure Functions 的驱动器
ERROR:
17.4.2020 13.06.39] java.sql.SQLException: No suitable driver found for jdbc:sqlserver://sql...;
[17.4.2020 13.06.39] Function "TopicTriggerSQLOutput" (Id: 70a1ce4c-3828-4280-81cf-dafa61956cb5)
invoked by Java Worker
[17.4.2020 13.06.39] at java.sql.DriverManager.getConnection(DriverManager.java:689)
CODE:
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:
https://docs.microsoft.com/en-us/sql/connect/jdbc/step-3-proof-of-concept-connecting-to-sql-
using-java?view=sql-server-ver15
*/
String connectionUrl = "jdbc:sqlserver://sql...";
ResultSet resultSet = null;
try (Connection connection = DriverManager.getConnection(connectionUrl);
Statement statement = connection.createStatement();) {
// 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));
}
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
//context.getLogger().info(message);
}
}
解决方案
对于驱动程序问题,我需要用 VS Code 做什么?
您需要将 jdbc 驱动程序依赖项添加到pom.xml
文件中。这是我用来连接到 Azure SQL 的依赖项示例。
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
</dependency>
参考:
推荐阅读
- javascript - 为什么这个点击事件处理程序没有执行我的代码?
- javascript - 使用带有谓词对象数组的 lodash 过滤器删除结果数据中的属性的更好方法是什么?
- javascript - 如何将对象数组添加到 HTML 表中?
- android - TouchableOpacity、负边距和 Android 的问题 - React Native
- blazor - 如何将两种方式绑定到子组件中的选择 - Blazor
- python - Python,如何用谷歌搜索结果显示页面?
- css - 是否可以在底部放置红宝石字体?
- discord - 尝试删除具有特定角色的用户发送的所有消息,除非该消息是 !verify
- javascript - 有没有办法将对象从对象推送到数组?(Vue)
- python - 从 jinja2 for 循环动态创建变量名的问题