2011-05-04 25 views
6

我已經下載JDK 6,也是我有sqljdb4.jar,我有database.properties文件內容如下數據JDBC例爲Java

 
database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver 
database.url=jdbc:sqlserver://.;databaseName=UserInfo;integratedSecurity=true; 
database.username=sa 
database.password=admin 

BN:我在我的機器上安裝服務器和服務器名稱=。 ,還我使用Windows Authontication

我現在的問題是,當我嘗試建立連接,我有以下錯誤

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: Connection refused: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)

我不知道什麼是這裏的確切的問題

如果任何一個可以幫助我可以理解提前

感謝

+1

請確保您在SQL Server配置管理器 - > SQL Server網絡配置 - >協議中啓用了TCP/IP – MDV2000 2011-05-04 15:11:09

+0

是的,我已啓用它,並且它也可以在端口1433上工作,但我也有相同的異常 – 2011-05-04 15:39:33

回答

5

這是許多概率造成像 1- IP是worong 2端口是錯誤的 3-有被防火牆阻止機器出去並連接到另一個IP 4- SQL服務器停機。

嘗試使用

public class JdbcSQLServerDriverUrlExample 
{ 
    public static void main(String[] args) 
    { 
    Connection connection = null; 
    try 
    { 
     // the sql server driver string 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

     // the sql server url 
     String url = "jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE"; 

     // get the sql server database connection 
     connection = DriverManager.getConnection(url,"THE_USER", "THE_PASSWORD"); 

     // now do whatever you want to do with the connection 
     // ... 

    } 
    catch (ClassNotFoundException e) 
    { 
     e.printStackTrace(); 
     System.exit(1); 
    } 
    catch (SQLException e) 
    { 
     e.printStackTrace(); 
     System.exit(2); 
    } 
    } 
} 

什麼,我需要解釋的是有很好的技術,稱爲「持久性」比JDBC更好,比輝煌和易於使用的更多。

+0

您能提供您提到的「持久性」技術的鏈接或全名嗎? – 2013-07-05 21:12:37

5

的問題是,你的SQL服務器是

  • 沒有安裝,
  • 沒有運行或
  • 不接受TCP/IP連接。

特別是最後一個是討厭的,因爲我記得有些版本的SQL Server沒有配置默認運行的TCP/IP連接器。

1

首先我們需要看你的代碼。其次看數據庫的錯誤信息是A)未運行
B)在不同的端口
或C)代碼不正確。