简体   繁体   English

Azure Java Web App将库添加到Classpath

[英]Azure Java Web App adding libraries to Classpath

I need to add sqljdbc42.jar library to classpath so I can connect to Azure SQL Database. 我需要将sqljdbc42.jar库添加到classpath,以便我可以连接到Azure SQL数据库。 How (if possible) can I do that in Azure Web Service - Web App (Java with Tomcat server). 如何(如果可能)我可以在Azure Web服务 - Web App(带有Tomcat服务器的Java)中执行此操作。 Is there any other way to add JDBC library? 有没有其他方法可以添加JDBC库?

Server error: 服务器错误:

    java.sql.SQLException: No suitable driver found for jdbc:sqlserver://XXXXXXX.database.windows.net:1433;database=XXXXXX;user=XXXXX@XXXXX;password=XXXXXXXX;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at common.DBConnection.<init>(DBConnection.java:23)
    at servlets.Login.doPost(Login.java:77)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Dec 30, 2015 3:04:48 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [servlets.Login] in context with path [/deviceweb] threw exception
java.lang.NullPointerException
    at common.DBConnection.addParameter(DBConnection.java:33)
    at servlets.Login.doPost(Login.java:78)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Web page error: 网页错误:

    type: Exception report

    message:

    description: The server encountered an internal error that prevented it from fulfilling this request.

    exception:

    java.lang.NullPointerException
        common.DBConnection.addParameter(DBConnection.java:33)
        servlets.Login.doPost(Login.java:78)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note: The full stack trace of the root cause is available in the Apache Tomcat/8.0.30 logs.

Connection string: 连接字符串:

String connectionString = "jdbc:sqlserver://testnabaza.database.windows.net:1433;" + "database=testnabaza;"
            + "user=butterbeast@testnabaza;" + "password=1!qQaaaaaa;" + "encrypt=true;"
            + "trustServerCertificate=false;" + "hostNameInCertificate=*.database.windows.net;" + "loginTimeout=30;";

Copied from Portal: jdbc:sqlserver://testnabaza.database.windows.net:1433;database=testnabaza;user=butterbeast@testnabaza;password={your_password_here};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30; 从Portal复制:jdbc:sqlserver://testnabaza.database.windows.net:1433; database = testnabaza; user = butterbeast @ testnabaza; password = {your_password_here}; encrypt = true; trustServerCertificate = false; hostNameInCertificate = * .database。 windows.net; loginTimeout = 30;

Easiest way around this was to simply add the sqljdbc42.jar file (probably located in C:\\Program Files\\Microsoft SQL Server JDBC Driver\\sqljdbc_3.X\\enu) to the \\lib\\ext directory of my local Java installation before creating the .zip file. 最简单的方法是在创建之前,将sqljdbc42.jar文件(可能位于C:\\ Program Files \\ Microsoft SQL Server JDBC Driver \\ sqljdbc_3.X \\ enu中)添加到本地Java安装的\\ lib \\ ext目录中。 .zip文件。

for best help use the given link https://dzone.com/articles/java-access-sql-azure-jdbc 为了获得最佳帮助,请使用给定的链接https://dzone.com/articles/java-access-sql-azure-jdbc

Per my experience, the simple way for adding the sqljdbc42.jar library (please download it from the offical site https://www.microsoft.com/en-US/download/details.aspx?id=11774 ) to classpath is that drag or copy the jdbc driver file to the directory WebContent/WEB-INF/lib of Dynamic Web Project in Eclipse, please see the picture below. 根据我的经验,添加sqljdbc42.jar库(请从官方网站https://www.microsoft.com/en-US/download/details.aspx?id=11774下载)到类路径的简单方法是将jdbc驱动程序文件拖放或复制到Eclipse中Dynamic Web Project WebContent/WEB-INF/lib目录,请参见下图。

在此输入图像描述

When you export the war file from the project in Eclipse, the library file will be included into it. 从Eclipse中的项目导出war文件时,库文件将包含在其中。 Then, you can deploy the war file into Azure WebApp for Java with Tomcat, and it will work fine. 然后,您可以使用Tomcat将war文件部署到Azure WebApp for Java中,它将正常工作。


The exception java.sql.SQLException: No suitable driver found in most situations is caused by the incorrect url. java.sql.SQLException: No suitable driver found异常java.sql.SQLException: No suitable driver found在大多数情况下java.sql.SQLException: No suitable driver found是由错误的url引起的。 Please check your connection string and correctly use it for Java jdbc programming. 请检查您的连接字符串并正确使用它进行Java jdbc编程。

As reference, Here is my simple code. 作为参考,这是我的简单代码。

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String jdbcUrl = "jdbc:sqlserver://<host>.database.windows.net:1433;database=<database>;user=<username>@<host>;password=<password>;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
String user = "<username>@<host>";
String password = "<password>";
Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
PreparedStatement statement = conn.prepareStatement("select 1 as id, 'peter' as name");
ResultSet rs = statement.executeQuery();
while(rs.next()) {
    System.out.println(rs.getLong("id")+","+rs.getString("name"));
}
rs.close();
statement.close();

I get the reply from Azure team which said that I is not possibe to solve this with Web App, but I need to create Cloud Service: 我收到了Azure团队的回复,他说我不太可能通过Web App解决这个问题,但我需要创建Cloud Service:

Unfortunately JDBC libraries fall into an area where Tomcat pretty much forces you to put them in CATALINA_HOME\\lib but then you need to make sure that the driver you put in will work for all your web apps in that Tomcat. 不幸的是,JDBC库属于Tomcat几乎强迫您将它们放入CATALINA_HOME \\ lib的区域,但是您需要确保您输入的驱动程序适用于该Tomcat中的所有Web应用程序。

From what I remember this is tied to Tomcat memory leak prevention which makes Tomcat only scan CATALINA_HOME\\lib once at Tomcat startup for database drivers. 从我记忆中,这与Tomcat内存泄漏预防有关,这使得Tomcat仅在Tomcat启动时扫描CATALINA_HOME \\ lib一次用于数据库驱动程序。 There are good reasons to do this tied to how the drivers register themselves with the JVM and how that impacts the classloader. 这样做有很好的理由与驱动程序如何在JVM中注册以及它如何影响类加载器有关。 Even if you wanted to disable this behaviro, you'd have to edit server.xml which would again drive you to have your own Tomcat install. 即使您想要禁用此行为,您也必须编辑server.xml,这将再次驱动您拥有自己的Tomcat安装。

So the result of my rambling narrative is that you need to install Tomcat from the gallery or upload your own version and then drop your JDBC drivers in your Tomcat\\lib directory. 因此,我漫无边际叙述的结果是您需要从库中安装Tomcat或上传您自己的版本,然后将您的JDBC驱动程序放在Tomcat \\ lib目录中。 You are trying to do more than you can with the UI enablement of Java for your app. 您正试图通过为您的应用程序启用Java的UI来做更多的事情。

https://azure.microsoft.com/en-us/documentation/articles/web-sites-java-add-app/ https://azure.microsoft.com/en-us/documentation/articles/web-sites-java-add-app/

There are outdated tutorials on internet but can help to figured out what to do. 互联网上有过时的教程,但可以帮助找出要做的事情。 subhash singh link helped me with general idea but the tutorial is outdated and a bit confusing. subhash singh链接帮助了我一般的想法,但教程已经过时,有点令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM