java - 无法使用 JDBC 连接到 Azure sql 数据库 - SSL 错误

标签 java android-studio ssl jdbc azure-sql-database

我正在尝试使用 Android Studio 通过移动应用程序连接到我的 Azure sql 数据库。

String connectionURL = "jdbc:sqlserver://****.database.windows.net:1433;database=****;user=****@****;password=*****;encrypt=false;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30";

public void connect(){

    Connection connection = null;

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        connection = DriverManager.getConnection(connectionURL);

    }
    catch (Exception e) {
        e.printStackTrace();
    }

这是我连接到 Azure 上的数据库的代码。已导入正确的 JDBC 驱动程序,我使用的是正确的用户名/密码(标记为 ***s)。 我不断收到关于 SSL 的相同错误,如下所示 -

04-03 23:04:49.449 31439-31439/com.example.melissa.dbtest W/System.err: com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed". ClientConnectionId:*****
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2435)
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1816)
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2022)
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1687)
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1528)
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:866)
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:569)
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:569)
04-03 23:04:49.452 31439-31439/com.example.melissa.dbtest W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:237)
04-03 23:04:49.453 31439-31439/com.example.melissa.dbtest W/System.err:     at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1753)

我看了这么多的答案和文档,但我似乎仍然无法解决问题。如何使用 SSL 连接到 Azure 数据库?

最佳答案

您可以尝试使用 JTDS 驱动程序吗

try {
        String SQL = "select * from dbo.Student";

        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String url = String.format("jdbc:jtds:sqlserver://***.database.windows.net:1433/<your database name>;user=***;password=***;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");
        conn = DriverManager.getConnection(url);

        stmt = conn.createStatement();
        rs = stmt.executeQuery(SQL);

        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }
        close();
    } catch (Exception e) {
        e.printStackTrace();
    }

关于java - 无法使用 JDBC 连接到 Azure sql 数据库 - SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49640223/

相关文章:

java - 在 android 中调用 API 并使用 Retrofit 2 收到此错误 :Response{protocol=h2, code=200, message=, url=https ://api. com/video/list-video.php}

android - HttpClient 依赖警告 Android Studio

asp.net - 在没有管理员帐户的情况下保护 ELMAH

php - 使用 sha256_password(访问被拒绝)时如何使用 PHP 的 mysqli 连接到 MySQL

c# - Windows Phone 8.0 证书固定

java - Spring + hibernate + HikariCP : how to handle DB connection while doing long running REST call?

java - 在 Jetty 上使用 Comet 长轮询进行持续推送?

java - Maven + JSTL 无法正常工作,所有配置均显示正常

java - 你能在android studio中将蓝牙套接字从一个 Activity 转移到另一个 Activity 吗

android - 从我的 Android 应用调用 Google Pay Intent 时出现错误 "You have exceeded the maximum transaction amount set by your bank"