gpt4 book ai didi

java - 在 Visual Studio Code 中部署 Azure Function 不包含 .jar 文件

转载 作者:行者123 更新时间:2023-12-01 17:16:01 25 4
gpt4 key购买 nike

我正在构建的 Azure 函数需要能够在 Azure SQL Server 数据库中执行过程。

我在 Eclipse 中有工作 Java 代码(基于 @duffmo 在 Java DB connection 中的回答)

然后,我将代码移植到 Visual Studio Code 中的 Azure 函数,以部署到 Azure。 (注意我已经删除了安全代码等)我使用 View/Command Palette/Azure Functions - Create New Project 创建了该项目

package com.function;

import java.sql.*;
import java.util.*;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

/**
* Azure Functions with HTTP Trigger.
*/
public class Function {
/**
* This function listens at endpoint "/api/HttpTrigger-Java". Two ways to invoke
* it using "curl" command in bash: 1. curl -d "HTTP Body" {your
* host}/api/HttpTrigger-Java&code={your function key} 2. curl "{your
* host}/api/HttpTrigger-Java?name=HTTP%20Query&code={your function key}"
* Function Key is not needed when running locally, it is used to invoke
* function deployed to Azure. More details:
* https://aka.ms/functions_authorization_keys
*/
private static final String DEFAULT_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String DEFAULT_URL = "jdbc:sqlserver://myserver.database.windows.net:1433;database=mydb;loginTimeout=10;user=myuser@myserver;password=mypassword;";


@FunctionName("HttpTrigger-Java")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {

Connection connection = null;
try {

connection = createConnection(DEFAULT_DRIVER, DEFAULT_URL,context);
connection.setAutoCommit(false);
String sqlUpdate = "{call MYDB.MYPROC(?,?}";
List<Object> parameters = Arrays.asList("Bar", "Foo");
execute(connection, sqlUpdate, parameters);
connection.commit();

} catch (Exception e) {
rollback(connection);
e.printStackTrace();
} finally {
close(connection);
}
return null;
}

public static Connection createConnection(String driver, String url, ExecutionContext context) throws ClassNotFoundException, SQLException {
Class.forName(driver);
return DriverManager.getConnection(url);
}

public static void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void close(Statement st) {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void rollback(Connection connection) {
try {
if (connection != null) {
connection.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
}

public static double execute(Connection connection, String sql, List<Object> parameters) throws SQLException {
CallableStatement call = connection.prepareCall(sql);
try {
int i = 0;
for (Object parameter : parameters) {
call.setObject(++i, parameter);
}
call.executeUpdate();

} finally {
close(call);
}
return 0;
}
}

但是行

 Class.forName(driver);

导致以下错误

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)

我尝试通过以下方式解决这个问题

  1. 输入 sqljdbc4.jar在“lib”目录中
  2. 手动将以下内容添加到 pom.xml

    <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency>

  3. 尝试install the jar从终端通过

    mvn install:install-file -Dfile=’C:=myPath\myFunction\lib\sqljdbc4.jar' -DgroupId=package -DartifactId=sqljdbc4 -Dversion='4.0' -Dpackaging=jar

  4. experimented更改 DEFAULT_DRIVER 字符串中“microsoft”和“sqlserver”的顺序。

  5. 尝试从新的 Java Dependencies view 添加 SQLJDBC (参见@hemangs答案)-但它没有出现在列表中

  6. 我按照 @asndr 在 .classpath 中的回答编辑了 .classPath - 请注意,我没有设法从 VS Code 内访问 .classPath,而是通过文件资源管理器 - 然后运行 ​​view/Command Palette/Java: Clean the java language server workspace

有什么想法吗?

最佳答案

基于@nirmal在Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0中的回答我做了以下事情:

  1. 资源管理器/Java 依赖项/Maven 依赖项 - 然后单击“+”
  2. 输入 mssql-jdbc并按 Enter 键
  3. 已选择mssql-jdbc来自 com.microsoft.sqlserver
  4. 这会在 VS Code 中打开 pom.xml,并添加以下内容

    <dependency>

      <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>8.3.0.jre14-preview</version>
    </dependency>
  5. 我将版本号更改为 6.1.0.jre8 (高版本导致编译错误)

  6. 已保存
  7. VS Code 问我是否要“修改构建文件”。您想要同步 Java 类路径/配置吗?'
  8. 我答应了,然后就成功了。

似乎最重要的是在 VS Code 中编辑 pom.xml。似乎当我在 VS Code 之外编辑它时,VS Code 没有触发配置同步。

关于java - 在 Visual Studio Code 中部署 Azure Function 不包含 .jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61386820/

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