2013-02-15 103 views
0

我想連接到sql server express。 我下載了驅動程序this。 我讀了幫助文件,這是我的代碼:如何解決它:com.microsoft.sqlserver.jdbc.SQLServerException?

 try 
     { 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      String connectionUrl = "jdbc:sqlserver://localhost;database=ActorsDb;integratedSecurity=true;"; 
      Connection con = DriverManager.getConnection(connectionUrl); 
      PreparedStatement st = con.prepareStatement("INSERT INTO Actors(FirstName,LastName,Age) VALUES(?,?,?)"); 
      st.setString(1, "Robert"); 
      st.setString(2, "de Niro"); 
      st.setInt(3,45); 
      st.executeUpdate();  
      con.close(); 
     } 
     catch (SQLException | ClassNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 

我得到這個異常:com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".

我關掉防火牆,但沒有任何改變。

  1. 我到SQL Server配置管理器,我啓用了TCP/IP

  2. 我去IP不會忽略,IP1並設置屬性

活動:是;啓用:是; TCP動態端口:[空]; TCP端口:1433

我錯過了什麼提示? 謝謝。

回答

0

問題已解決。我不得不在工作目錄中複製sqljdbc_auth.dll。

+0

我想連接SQL Server與配置單元使用sqoop'[root @ SLAVENODE1 sqoop/bin]#sqoop import --connect「jdbc:sqlserver://10.23.52.15:1433; dtabase = test; username = **** ; password = ****「--table dept -m 1 --hive-import - --schema dbo',發生上述相同的錯誤。我在sqoop/lib文件夾中移動了sqljdbc4.jar。正如你所說的那樣複製sqljdbc_auth.dll。請幫助 – 2016-08-26 08:08:21

0

檢查SQL Server服務是否正在運行,並檢查防病毒程序是否阻塞了連接。如果是這樣,請禁用它們或向該連接添加異常。

+0

Sql服務器服務正在運行,因爲我在進行更改後重新啓動它。我沒有防病毒程序。 – 2013-02-15 18:17:19

0

您正在JDBC URL中使用localhost,請嘗試使用您在SSCM中描述的「IP1」的IP地址。或者,驗證SSCM已配置爲在本地主機上偵聽:1433。