2016-06-10 69 views
0

我嘗試使用連接到Azure的SQL:如何連接到Azure的SQL數據庫JTDS

import java.sql.*; 

public class ExampleJTDS { 

    public static void main(String[] args) { 

     // Setting. 
     String connectionUrl = "jdbc:jtds:sqlserver://SERVER.database.windows.net:1433/DATABASE;ssl=off"; 
     String user = "[email protected]"; 
     String pass = "PASSWORD"; 

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

     try { 
      // Establish the connection. 
      Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
      conn = DriverManager.getConnection(connectionUrl, user, pass); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

但我得到:

java.sql.SQLException: I/O Error: DB server closed connection. 
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2481) 
    at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:632) 
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:371) 
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) 
    at java.sql.DriverManager.getConnection(DriverManager.java:664) 
    at java.sql.DriverManager.getConnection(DriverManager.java:247) 
    at run.ExampleJTDS.main(ExampleJTDS.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) 

如果我強迫加密代SSL =關閉SSL =需要,我得到:

java.sql.SQLException: Network error IOException: Connection reset 
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436) 
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) 
    at java.sql.DriverManager.getConnection(DriverManager.java:664) 
    at java.sql.DriverManager.getConnection(DriverManager.java:247) 
    at run.ExampleJTDS.main(ExampleJTDS.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) 

有趣的是,我可以通過同一臺計算機和相同的JDBC驅動程序連接到數據庫,其中SQuirreL SQL(儘管沒有SSL - SQuirreL SQL設法將證書放入第一個TDS數據包,Azure接受這個數據庫)。因此,這個問題不應該放在防火牆的設置中。

元數據:

  • 服務器:天青V12
  • 驅動:JTDS-1.3.1
  • JRE:1.8.0_72-B15(從Oracle)
  • _JAVA_OPTIONS:-Djsse.enableCBCProtection =假
  • security.provider.1:sun.security.provider.Sun
  • OS:OS X 10.11.5
  • SQuirreL SQL:3.7.1

如何從Java連接到Azure SQL?

+0

[如何使用JDBC連接到Azure SQL](http://stackoverflow.com/questions/37743819/how-to-connect-to-azure-sql-with-jdbc) –

回答

0

根據我的經驗,我認爲問題是由連接字符串造成的,該連接字符串是代碼的變量connectionUrl。我已經回答了您關於SO線程的類似問題,請參閱How to connect to Azure SQL with JDBC

但是,使用jTDS代替微軟的JDBC驅動程序對於SQL Server有一點差異,你可以參考the step 3 of the tutorial中的註釋來了解它。作爲參考,我在這裏張貼筆記的內容。

注:

如果您使用的是JTDS JDBC驅動程序,那麼您就需要「SSL =需要」添加到連接字符串的URL,你需要設置以下選項JVM「-Djsse.enableCBCProtection = false」。此JVM選項禁用針對安全漏洞的修復程序,因此在設置此選項之前,請確保您瞭解涉及哪些風險。

希望它有幫助。任何問題,請隨時讓我知道。

+0

@ user824276任何更新? –