无法使用JDBC连接到Azure SQL数据库 - SSL出错

3

我正在尝试通过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数据库?
1个回答

0

你能否尝试使用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();
    }

非常感谢,这个完美地解决了我的问题!一旦我添加了JTDS驱动程序并更改了连接字符串,它就可以正常工作了。 - Mel H

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接