2012-04-05 56 views
2

我知道這是一個重複的問題。我發現了非常類似的問題和解決方案,但我仍然對它感到震驚。連接到SQL服務器時的SQL異常

我使用eclipse連接我的java應用程序與微軟sql server 2008數據庫。下面是我的代碼

import java.sql.*; 
public class ConnectionTest2 { 
    public static void main(String [] args) { 
    try { 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
     String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=Sample;integratedSecurity=true"; 
     Connection con = DriverManager.getConnection(connectionUrl,"",""); 
     System.out.println("Connected"); 
     } catch (SQLException e) { 
      System.out.println("SQL Exception: "+ e.toString()); 
     } 
catch (ClassNotFoundException cE) { 
      System.out.println("Class Not Found Exception: "+ cE.toString()); 
     } 
    } 
} 

我已經去到SQL Server Configuration Manager和IPALL下的端口號設置爲1433啓用TCP/IP和VIA。

我試過很多方法,但我無法找到一個解決方案,以下面的錯誤

SQL Exception: 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.".

我還禁用了Windows防火牆,但是無法連接。

請幫助我。

+0

你有沒有防病毒軟件或VPN軟件可能也有防火牆? – 2012-04-05 20:12:22

+0

這可能太基本了,但您確定該端口是否正確且可用? 在cmd中嘗試命令* telnet localhost 1433 *以確保 – 2012-04-05 20:14:15

+0

可能重複[如何使用JDBC連接到Java中的SQL Server 2008數據庫?](http://stackoverflow.com/questions/2451892/how -do -i-connect-to-a-sql-server-2008-database-in-java -with-jdbc) – 2012-04-05 20:15:08

回答

2

這就是它說的:連接被拒絕。你嘗試過'telnet 1433'嗎?

+0

_telnet localhost 1433_ says _Could not open connection to the host,on port 1433:Connect failed _... where我錯了嗎 – user1074122 2012-04-05 22:05:39

+0

您試圖連接到服務器未偵聽的端口時出錯。如果門關着,你還會用頭撞到它嗎? – ahanin 2012-04-06 08:33:23

3

我有這個問題,我通過啓用服務器實例的TCP/IP協議來修復它。你可以做到這一點的

  1. 打開SQL Server配置管理器
  2. 展開SQL Server網絡配置
  3. 選擇MSSQLSERVER的協議
  4. 啓用TCP/IP

在此之後,您需要重新啓動SQL Server服務以使其生效。

+0

試過了。不起作用。 – 2012-08-05 21:51:40

+0

這幫了我。這應該是真正的答案。 – Gallen 2014-02-04 19:49:56