Azure将java Web App(java)连接到SQLDatabase

时间:2015-12-11 09:22:50

标签: java sql-server azure jdbc

尝试在Azure中将Web App - Java与SQL数据库连接起来。 我用这个例子和这个页面: https://azure.microsoft.com/en-us/documentation/articles/sql-database-develop-java-simple-windows/

我复制粘贴我的连接字符串并将我的IP添加到SQL服务器/ mydatabase / settings / firewall settings

当我从Tomcat8(Eclipse)运行它时 - 本地,我得到了底部错误。

我可以使用conn中使用的User和Pass连接SQL Management Studio。字符串所以访问数据应该是正确的。 我还添加了从文章链接下载的sqljdbc4.jar lib。

  

java.sql.SQLException:找不到合适的jdbc驱动程序:sqlserver://mydatabase.database.windows.net:1433; database = mydatabase; user = [Server admin] @mydatabase; password = [MyAdminPassword]; encrypt = TRUE; trustServerCertificate = FALSE; hostNameInCertificate = * database.windows.net; loginTimeout = 30;       在java.sql.DriverManager.getConnection(DriverManager.java:689)       在java.sql.DriverManager.getConnection(DriverManager.java:270)       at customerwebservice.SQLDatabaseTest.getDatabase(SQLDatabaseTest.java:30)       at customerwebservice.GetCustomers.doGet(GetCustomers.java:34)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:622)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)       在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)       在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:674)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)       在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)       在java.lang.Thread.run(Thread.java:745)

代码:

import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;

public class SQLDatabaseTest {

public String getDatabase(){

    String connectionString = "";

    connectionString = "jdbc:sqlserver://mydatabase.database.windows.net:1433;database=mydatabase;user=[Server admin]@mydatabase;password=[My Password];encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";

    // Declare the JDBC objects.
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    PreparedStatement prepsInsertPerson = null;
    PreparedStatement prepsUpdateAge = null;

    try {           
        connection = DriverManager.getConnection(connectionString);

        return "Connected";      
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        // Close the connections after the data has been handled.
        if (prepsInsertPerson != null) try { prepsInsertPerson.close(); } catch(Exception e) {}
        if (prepsUpdateAge != null) try { prepsUpdateAge.close(); } catch(Exception e) {}
        if (resultSet != null) try { resultSet.close(); } catch(Exception e) {}
        if (statement != null) try { statement.close(); } catch(Exception e) {}
        if (connection != null) try { connection.close(); } catch(Exception e) {}
    }

   return "Not Connected"; 


   }
}

编辑: 我弄清楚出了什么问题。因为我使用的是Mac,所以我没有将sqljdbc42.jar文件添加到Classpath中。在Windows中你只需运行.exe但在Mac上你需要手动添加它。

这个答案可以帮助我将它添加到Classpath:https://stackoverflow.com/a/1676261/1436690

但是现在我还有另外一个问题,因为我还需要将sqljdbc42.jar添加到Azure Web App类路径中。

1 个答案:

答案 0 :(得分:0)

我弄清楚出了什么问题。因为我使用的是Mac,所以我没有将sqljdbc42.jar文件添加到Classpath中。在Windows中你只需运行.exe但在Mac上你需要手动添加它。

这个答案可以帮助我将它添加到Classpath: https://stackoverflow.com/a/1676261/1436690