- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建的 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)
我尝试通过以下方式解决这个问题
sqljdbc4.jar
在“lib”目录中手动将以下内容添加到 pom.xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
尝试install the jar从终端通过
mvn install:install-file -Dfile=’C:=myPath\myFunction\lib\sqljdbc4.jar'
-DgroupId=package -DartifactId=sqljdbc4 -Dversion='4.0' -Dpackaging=jar
experimented更改 DEFAULT_DRIVER 字符串中“microsoft”和“sqlserver”的顺序。
尝试从新的 Java Dependencies view 添加 SQLJDBC (参见@hemangs答案)-但它没有出现在列表中
我按照 @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中的回答我做了以下事情:
mssql-jdbc
并按 Enter 键mssql-jdbc
来自 com.microsoft.sqlserver这会在 VS Code 中打开 pom.xml,并添加以下内容
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.3.0.jre14-preview</version>
</dependency>
我将版本号更改为 6.1.0.jre8
(高版本导致编译错误)
似乎最重要的是在 VS Code 中编辑 pom.xml。似乎当我在 VS Code 之外编辑它时,VS Code 没有触发配置同步。
关于java - 在 Visual Studio Code 中部署 Azure Function 不包含 .jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61386820/
我是一名优秀的程序员,十分优秀!