简体   繁体   English

如何使用JDBC连接到Azure SQL

[英]How to connect to Azure SQL with JDBC

I try to connect to Azure SQL with MS JDBC driver: 我尝试使用MS JDBC驱动程序连接到Azure SQL:

import java.sql.*;

public class ExampleSQLJDBC {

    public static void main(String[] args) {

        // Setting.
        String connectionUrl = "jdbc:sqlserver://SERVER.database.windows.net:1433;database=DATABASE;encrypt=true;trustServerCertificate=true;";
        String user = "USER@SERVER";
        String pass = "PASSWORD";

        // Declare the JDBC object.
        Connection conn = null;

        try {
            // Establish the connection.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection(connectionUrl, user, pass);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

But I am getting: 但我得到:

com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Broken pipe ClientConnectionId:742ba7b5-43bb-441b-8e8d-bbb9dc5fa8ce".
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2226)
    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1756)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1803)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1454)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1285)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1131)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at run.ExampleSQLJDBC.main(ExampleSQLJDBC.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Interestingly, I can connect to the database from the same computer with Navicat. 有趣的是,我可以使用Navicat从同一台计算机连接到数据库。 Hence, the problem should not be in the setting of firewalls. 因此,问题不应该在于防火墙的设置。

Metadata: 元数据:

  • Server: Azure V12 服务器:Azure V12
  • Driver: 6.0 (sqljdbc42.jar) 驱动程序:6.0(sqljdbc42.jar)
  • JRE: 1.8.0_72-b15 (from Oracle) JRE:1.8.0_72-b15(来自Oracle)
  • _JAVA_OPTIONS: -Djsse.enableCBCProtection=false _JAVA_OPTIONS:-Djsse.enableCBCProtection = false
  • security.provider.1: sun.security.provider.Sun security.provider.1:sun.security.provider.Sun
  • OS: OS X 10.11.5 作业系统:OS X 10.11.5

How can I connect to Azure SQL from Java? 如何从Java连接到Azure SQL?

Edit: 编辑:

If I compile the code from Peter Pan with javac 1.8.0_92 on OS X and execute the bytecode with java 1.8.0_92 on OS X, the error does not disappear. 如果我在OS X上使用javac 1.8.0_92从Peter Pan编译代码,并在OS X上使用java 1.8.0_92执行字节码,则错误不会消失。

Edit 2: When I debuged the SSL connection details with -Djavax.net.debug=ssl:handshake:verbose : 编辑2:当我使用-Djavax.net.debug=ssl:handshake:verbose调试SSL连接详细-Djavax.net.debug=ssl:handshake:verbose

trigger seeding of SecureRandom
done seeding SecureRandom
Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA256
Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
Ignoring unavailable cipher suite: TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
Ignoring unavailable cipher suite: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
Allow unsafe renegotiation: false
Allow legacy hello messages: true
Is initial handshake: true
Is secure renegotiation: false
Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_128_CBC_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 for TLSv1
Ignoring unsupported cipher suite: TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 for TLSv1.1
Ignoring unsupported cipher suite: TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 for TLSv1.1
Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_128_CBC_SHA256 for TLSv1.1
Ignoring unsupported cipher suite: TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 for TLSv1.1
Ignoring unsupported cipher suite: TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 for TLSv1.1
Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 for TLSv1.1
Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 for TLSv1.1
%% No cached client session
*** ClientHello, TLSv1.2
RandomCookie:  GMT: 1466081162 bytes = { 238, 135, 254, 162, 132, 164, 75, 240, 96, 107, 17, 103, 60, 109, 229, 149, 31, 110, 218, 87, 49, 29, 121, 114, 115, 6, 44, 32 }
Session ID:  {}
Cipher Suites: [TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]
Compression Methods:  { 0 }
Extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1, secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect239k1, secp256k1}
Extension ec_point_formats, formats: [uncompressed]
Extension signature_algorithms, signature_algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA224withECDSA, SHA224withRSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA
Extension server_name, server_name: [type=host_name (0), value=hlt3qa7607.database.windows.net]
***
main, WRITE: TLSv1.2 Handshake, length = 233
main, called close()
main, called closeInternal(true)
main, SEND TLSv1.2 ALERT:  warning, description = close_notify
main, WRITE: TLSv1.2 Alert, length = 2
main, called closeSocket(true)
main, waiting for close_notify or alert: state 5
main, received EOFException: ignored
main, called closeInternal(false)
main, close invoked again; state = 5
main, handling exception: java.io.IOException: SQL Server did not return a response. The connection has been closed. ClientConnectionId:1eb03958-1566-4844-b842-e020f0c36121
main, called closeSocket()
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:1eb03958-1566-4844-b842-e020f0c36121".... 

Edit 3: After setting the driver to log all: 编辑3:将驱动程序设置为全部记录后:

Jun 17, 2016 9:26:37 AM com.microsoft.sqlserver.jdbc.SQLServerDriver:1 connect
FINER: ENTRY Arguments not traced.
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.Util parseUrl
FINE: Property:serverName Value:hlt3qa7607.database.windows.net
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.Util parseUrl
FINE: Property:portNumber Value:1433
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.Util parseUrl
FINE: Property:databaseName Value:DATABASE
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.Util parseUrl
FINE: Property:encrypt Value:true
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.Util parseUrl
FINE: Property:trustServerCertificate Value:true
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
FINE: ConnectionID:1 created by (SQLServerDriver:1)
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection login
FINER: ConnectionID:1 Start time: 1466148398058 Time out time: 1466148413058 Timeout Unit Interval: 1200
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection login
FINE: ConnectionID:1 This attempt server name: hlt3qa7607.database.windows.net port: 1433 InstanceName: null useParallel: false
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection login
FINE: ConnectionID:1 This attempt endtime: 1466148399258
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection login
FINE: ConnectionID:1 This attempt No: 0
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection connectHelper
FINE: ConnectionID:1 Connecting with server: hlt3qa7607.database.windows.net port: 1433 Timeout slice: 1194 Timeout Full: 15
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.TDSChannel open
FINER: TDSChannel (ConnectionID:1): Opening TCP socket...
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
FINER: ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c Requesting encryption level:ON
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
FINER: ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c ActivityId 866c0053-25be-4eb2-90c7-a40b5b0535a4-1
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.TDSChannel logPacket
FINEST: /192.168.1.186:63328 SPID:0 ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c Prelogin request                                         ...
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.TDSChannel logPacket
FINEST: /192.168.1.186:63328 SPID:0 ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c Prelogin response
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
FINE: ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c Server returned major version:12
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
FINER: ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c Negotiated encryption level:ON
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.SQLServerConnection Prelogin
FINER: ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c Ignoring prelogin response option:5
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
FINER: TDSChannel (ConnectionID:1) Enabling SSL...
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
FINER: TDSChannel (ConnectionID:1) SSL handshake will trust any certificate
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
FINEST: TDSChannel (ConnectionID:1) Getting TLS or better SSL context
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
FINEST: TDSChannel (ConnectionID:1) Initializing SSL context
Jun 17, 2016 9:26:38 AM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
FINEST: TDSChannel (ConnectionID:1) Creating SSL socket
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxySocket getInputStream
FINEST: TDSChannel (ConnectionID:1) (ProxySocket): Getting input stream
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxySocket getOutputStream
FINEST: TDSChannel (ConnectionID:1) (ProxySocket): Getting output stream
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
FINER: TDSChannel (ConnectionID:1) Starting SSL handshake
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyOutputStream writeInternal
FINEST: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyOutputStream@13c78c0b Writing 238 bytes
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeOutputStream writeInternal
FINEST: TDSChannel (ConnectionID:1) (SSLHandshakeOutputStream): Starting new TDS packet...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSWriter writeBytes
FINEST: TDSWriter@41629346 (ConnectionID:1) Writing 8 bytes
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeOutputStream writeInternal
FINEST: TDSChannel (ConnectionID:1) (SSLHandshakeOutputStream): Writing 238 bytes...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSWriter writeBytes
FINEST: TDSWriter@41629346 (ConnectionID:1) Writing 238 bytes
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyOutputStream flush
FINEST: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyOutputStream@13c78c0b Flushing
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeOutputStream flush
FINEST: TDSChannel (ConnectionID:1) (SSLHandshakeOutputStream): Ignored a request to flush the stream
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream readInternal
FINEST: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream@12843fce Reading 5 bytes
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream readInternal
FINEST: TDSChannel (ConnectionID:1) (SSLHandshakeInputStream): Reading 5 bytes...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream ensureSSLPayload
FINEST: TDSChannel (ConnectionID:1) (SSLHandshakeInputStream): No handshake response bytes available. Flushing SSL handshake output stream.
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeOutputStream endMessage
FINEST: TDSChannel (ConnectionID:1) (SSLHandshakeOutputStream): Finishing TDS message
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSWriter endMessage
FINEST: TDSWriter@41629346 (ConnectionID:1) Finishing TDS message
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel logPacket
FINEST: /0:0:0:0:0:0:c0a8:1ba:63328 SPID:0 TDSWriter@41629346 (ConnectionID:1) sending packet (246 bytes)
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel write
FINER: TDSChannel (ConnectionID:1) write failed:Broken pipe
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerException logException
FINE: *** SQLException:ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c com.microsoft.sqlserver.jdbc.SQLServerException: Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerException logException
FINE: com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2226)com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2210)com.microsoft.sqlserver.jdbc.TDSChannel.write(IOBuffer.java:1894)com.microsoft.sqlserver.jdbc.TDSWriter.flush(IOBuffer.java:4285)com.microsoft.sqlserver.jdbc.TDSWriter.writePacket(IOBuffer.java:4186)com.microsoft.sqlserver.jdbc.TDSWriter.endMessage(IOBuffer.java:3192)com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeOutputStream.endMessage(IOBuffer.java:824)com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.ensureSSLPayload(IOBuffer.java:687)com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.readInternal(IOBuffer.java:762)com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.read(IOBuffer.java:754)com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.readInternal(IOBuffer.java:949)com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.read(IOBuffer.java:937)sun.security.ssl.InputRecord.readFully(InputRecord.java:465)sun.security.ssl.InputRecord.read(InputRecord.java:503)sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1682)com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1803)com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1454)com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1285)com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1131)java.sql.DriverManager.getConnection(DriverManager.java:664)java.sql.DriverManager.getConnection(DriverManager.java:247)ExampleSQLJDBC.main(ExampleSQLJDBC.java:44)
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerConnection:1 close
FINER: ENTRY
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel disableSSL
FINER: TDSChannel (ConnectionID:1) Disabling SSL...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel disableSSL
FINEST: TDSChannel (ConnectionID:1) Rewiring proxy streams for SSL socket close
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel disableSSL
FINER: TDSChannel (ConnectionID:1) Closing SSL socket
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyOutputStream writeInternal
FINEST: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyOutputStream@13c78c0b Writing 7 bytes
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyOutputStream flush
FINEST: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyOutputStream@13c78c0b Flushing
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream readInternal
FINEST: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream@12843fce Reading 5 bytes
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream readInternal
FINEST: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream@12843fce Read -1 bytes
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel disableSSL
FINER: TDSChannel (ConnectionID:1) SSL disabled
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINEST: TDSChannel (ConnectionID:1): Closing inputStream...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINEST: TDSChannel (ConnectionID:1): Closing outputStream...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINER: TDSChannel (ConnectionID:1): Closing TCP socket...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerConnection:1 close
FINER: RETURN
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream ensureSSLPayload
FINER: TDSChannel (ConnectionID:1) (SSLHandshakeInputStream): Ending TDS message threw exception:Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream readInternal
FINER: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream@12843fce Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream readInternal
FINER: com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream@12843fce Reading bytes threw exception:Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel$ProxySocket close
FINER: TDSChannel (ConnectionID:1) (ProxySocket): Ignoring close
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
FINER: Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c
java.io.IOException: Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c
    at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.ensureSSLPayload(IOBuffer.java:692)
    at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.readInternal(IOBuffer.java:762)
    at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.read(IOBuffer.java:754)
    at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.readInternal(IOBuffer.java:949)
    at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.read(IOBuffer.java:937)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1682)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1803)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1454)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1285)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1131)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at ExampleSQLJDBC.main(ExampleSQLJDBC.java:44)

Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel enableSSL
FINER: java.security path: /Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/jre/lib/security
Security providers: [SUN version 1.8, SunRsaSign version 1.8, SunEC version 1.8, SunJSSE version 1.8, SunJCE version 1.8, SunJGSS version 1.8, SunSASL version 1.8, XMLDSig version 1.8, SunPCSC version 1.8, Apple version 1.8]
SSLContext provider info: Sun JSSE provider(PKCS12, SunX509/PKIX key/trust factories, SSLv3/TLSv1/TLSv1.1/TLSv1.2)
SSLContext provider services:
[SunJSSE: KeyFactory.RSA -> sun.security.rsa.RSAKeyFactory
  aliases: [1.2.840.113549.1.1, OID.1.2.840.113549.1.1]
, SunJSSE: KeyPairGenerator.RSA -> sun.security.rsa.RSAKeyPairGenerator
  aliases: [1.2.840.113549.1.1, OID.1.2.840.113549.1.1]
, SunJSSE: Signature.MD2withRSA -> sun.security.rsa.RSASignature$MD2withRSA
  aliases: [1.2.840.113549.1.1.2, OID.1.2.840.113549.1.1.2]
, SunJSSE: Signature.MD5withRSA -> sun.security.rsa.RSASignature$MD5withRSA
  aliases: [1.2.840.113549.1.1.4, OID.1.2.840.113549.1.1.4]
, SunJSSE: Signature.SHA1withRSA -> sun.security.rsa.RSASignature$SHA1withRSA
  aliases: [1.2.840.113549.1.1.5, OID.1.2.840.113549.1.1.5, 1.3.14.3.2.29, OID.1.3.14.3.2.29]
, SunJSSE: Signature.MD5andSHA1withRSA -> sun.security.ssl.RSASignature
, SunJSSE: KeyManagerFactory.SunX509 -> sun.security.ssl.KeyManagerFactoryImpl$SunX509
, SunJSSE: KeyManagerFactory.NewSunX509 -> sun.security.ssl.KeyManagerFactoryImpl$X509
  aliases: [PKIX]
, SunJSSE: TrustManagerFactory.SunX509 -> sun.security.ssl.TrustManagerFactoryImpl$SimpleFactory
, SunJSSE: TrustManagerFactory.PKIX -> sun.security.ssl.TrustManagerFactoryImpl$PKIXFactory
  aliases: [SunPKIX, X509, X.509]
, SunJSSE: SSLContext.TLSv1 -> sun.security.ssl.SSLContextImpl$TLS10Context
  aliases: [SSLv3]
, SunJSSE: SSLContext.TLSv1.1 -> sun.security.ssl.SSLContextImpl$TLS11Context
, SunJSSE: SSLContext.TLSv1.2 -> sun.security.ssl.SSLContextImpl$TLS12Context
, SunJSSE: SSLContext.TLS -> sun.security.ssl.SSLContextImpl$TLSContext
  aliases: [SSL]
, SunJSSE: SSLContext.Default -> sun.security.ssl.SSLContextImpl$DefaultSSLContext
, SunJSSE: KeyStore.PKCS12 -> sun.security.pkcs12.PKCS12KeyStore
]
java.ext.dirs: /Users/jan/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerException logException
FINE: *** SQLException:ConnectionID:1 ClientConnectionId: 1a3032c8-493d-4f99-8641-d0f02211d82c com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c". The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Broken pipe ClientConnectionId:1a3032c8-493d-4f99-8641-d0f02211d82c".
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerException logException
FINE: com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2226)com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1756)com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1803)com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1454)com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1285)com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1131)java.sql.DriverManager.getConnection(DriverManager.java:664)java.sql.DriverManager.getConnection(DriverManager.java:247)ExampleSQLJDBC.main(ExampleSQLJDBC.java:44)
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerConnection:1 close
FINER: ENTRY
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINEST: TDSChannel (ConnectionID:1): Closing inputStream...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINEST: TDSChannel (ConnectionID:1): Closing outputStream...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINER: TDSChannel (ConnectionID:1): Closing TCP socket...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerConnection:1 close
FINER: RETURN
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerConnection:1 close
FINER: ENTRY
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINEST: TDSChannel (ConnectionID:1): Closing inputStream...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINEST: TDSChannel (ConnectionID:1): Closing outputStream...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.TDSChannel close
FINER: TDSChannel (ConnectionID:1): Closing TCP socket...
Jun 17, 2016 9:26:43 AM com.microsoft.sqlserver.jdbc.SQLServerConnection:1 close
FINER: RETURN

The problem was solved with these steps: 通过以下步骤解决了该问题:

  1. Open terminal (/Applications/Utilities/Terminal) 打开终端(/应用程序/实用程序/终端)
  2. Run scutil --get HostName (case sensitive!) 运行scutil --get HostName (区分大小写!)
  3. If hostname is not set or contains .local, run sudo scutil --set HostName "newname" 如果未设置主机名或包含.local,请运行sudo scutil --set HostName "newname"
  4. Try JDBC Azure SQL Connection again - hostname change is effective immediately 再次尝试JDBC Azure SQL连接-主机名更改立即生效

Source: https://youtrack.jetbrains.com/issue/JRE-221#comment=27-1826316 资料来源: https : //youtrack.jetbrains.com/issue/JRE-221#comment=27-1826316

Per my experience, I think the issue was caused by the connection string which is the variable connectionUrl of your code. 根据我的经验,我认为问题是由连接字符串引起的,连接字符串是代码的变量connectionUrl

Please see the completed connection string below. 请在下面查看完整的连接字符串。

jdbc:sqlserver://<hostname>.database.windows.net:1433;database=<database-name>;user=<username>@<hostname>;password={your_password_here};encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;

You can find it at the link Show connection strings of the right bar of the tab DASHBOARD on Azure old portal (see Fig 1), or see it at the link Show database connection strings on Azure new portal (see Fig 2). 您可以在Azure旧门户上的“ DASHBOARD ”选项卡的右栏中Show connection strings的链接Show connection strings找到它(请参见图1),也可以在Azure新门户上的“ Show database connection strings ”链接Show database connection strings看到它(请参见图2)。

Fig 1. Show connection string on Azure old portal 图1.在Azure旧门户上显示连接字符串

在此处输入图片说明

Fig 2. Show database connection strings on Azure new portal 图2.在Azure新门户上显示数据库连接字符串

在此处输入图片说明

Hope it helps. 希望能帮助到你。 Any concern, please feel free to let me know. 如有任何疑问,请随时告诉我。


Update 更新

Here is my code for connecting SQL Azure. 这是我用于连接SQL Azure的代码。

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    String hostName = "xxxxx";
    String dbName = "petersqldb";
    String user = "peter@xxxx";
    String password = "xxxxxxxx";
    String url = String.format("jdbc:sqlserver://%s.database.windows.net:1433;database=%s;user=%s;password=%s;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;", hostName, dbName, user, password);
    Connection conn = DriverManager.getConnection(url, user, password);
    Statement stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("select 1+1 as sum");
    while(rs.next()) {
        System.out.println(rs.getInt("sum"));
    }
    rs.close();
    stat.close();
    conn.close();
}

This code works well: to connect Azure database and grab data from table 这段代码很好用:连接Azure数据库并从表中获取数据

function onOpen() {

    var spreadsheet = SpreadsheetApp.getActive();

    var menuItems = [
        {name: 'Get Data', functionName: 'readData'}
    ];
        spreadsheet.addMenu('Report', menuItems);
    }


    // Replace the variables in this block with your values.
     var hostName = 'SERVER.database.windows.net:1433;'
     var db = 'DBNAME;';
     var user = 'USER@SERVER';
     var userPwd = 'PASSWORD';



     var dbUrl = 'jdbc:sqlserver://'+hostName + 'databaseName='+db; 


    function readData() {
     var conn = Jdbc.getConnection(dbUrl, user, userPwd);
     var stmt = conn.createStatement();


     // Place your query below
     var results = stmt.executeQuery('SELECT TOP (10) * FROM [dbo].[NAME]');
     var metaData=results.getMetaData();
     var numCols = metaData.getColumnCount();
     var sheet = SpreadsheetApp.getActiveSheet();

     sheet.clearContents();

     var arr=[];

     for (var col = 0; col < numCols; col++) {
      arr.push(metaData.getColumnName(col + 1));
     }

     sheet.appendRow(arr);


     while (results.next()) {
      arr=[];

      for (var col = 0; col < numCols; col++) {

       arr.push(results.getString(col + 1));
      }
     sheet.appendRow(arr);

    }

    results.close();
    stmt.close();

    sheet.autoResizeColumns(1, numCols+1);

    }

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

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