使用Java Web App连接到SQL Server时出现问题
我有一个连接到Azure上的sql server db的Java应用程序,当我在本地运行该应用程序时,我没有任何连接问题.但是,当我将应用程序部署到运行tomcat的Azure Web应用程序时,出现以下错误:
I have a java application that connects to a sql server db on Azure, when I run the application locally I have no issues connecting. However when I deploy my application to Azure web app running tomcat I get the following error:
java.sql.SQLException:未找到适用于jdbc:sqlserver://
的驱动程序
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://
在我的pom.xml文件中
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.0.0.jre10</version>
</dependency>
我可以在web-inf/lib文件夹下看到sqlserver jar文件,这是创建连接的Java代码:
And I can see the sqlserver jar file under web-inf/lib folder, and here is the java code creating the connection:
String url = String.format("jdbc:sqlserver://%s:1433;database=%s;user=%s;password=%s;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;", hostName, dbName, user, password);
connection = DriverManager.getConnection(url);
If you haven’t tried the following steps already, kindly perform the below suggestions and let us know if that makes any difference:
- 从您的app/lib文件夹中删除sqljdbc * .jar文件.
- 如果您使用的是自定义Tomcat或Azure Marketplace Tomcat Web服务器,请将此.jar文件复制到Tomcat
- 如果您要通过Azure门户启用Java(请选择 Java 1.8 ; Tomcat服务器),将sqljdbc.* jar文件复制到 与您的应用程序平行.然后,将以下类路径设置添加到web.config文件:
- Remove the sqljdbc*.jar file from your app/lib folder.
- If you are using the custom Tomcat or Azure Marketplace Tomcat web server, copy this .jar file to the Tomcat lib folder.
- If you are enabling Java from the Azure portal (select Java 1.8 > Tomcat server), copy the sqljdbc.* jar file in the folder that's parallel to your app. Then, add the following classpath setting to the web.config file:
< httpPlatform>
< environmentVariables>
< environmentVariablename ="JAVA_OPTS";值=" -Djava.net.preferIPv4Stack = true
-Xms128M -classpath%CLASSPATH%; [sqljdbc * .jarfile的路径] />
</environmentVariables>
</httpPlatform>
<httpPlatform>
<environmentVariables>
<environmentVariablename ="JAVA_OPTS" value=" -Djava.net.preferIPv4Stack=true
-Xms128M -classpath %CLASSPATH%;[Path to the sqljdbc*.jarfile]" />
</environmentVariables>
</httpPlatform>