2014-02-17 124 views
1

好了,所以我有這樣的代碼:的Java連接到SQL Server

package com.andrewxd.banksystem; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 

public class Interface 
    { 
     public static void main(String[] args) 
     { 
      try 
      { 
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
       Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1443;user=Andrew;password=andrei23;database=BankSystem"); 
       System.out.println("test"); 
       Statement sta = conn.createStatement(); 
       String Sql = "select * from Clients"; 
       ResultSet rs = sta.executeQuery(Sql); 

       System.out.println(rs.next()); 
      } catch (Exception e){ 
       e.printStackTrace(); 
      } 
    } 


} 

,但它給了我這個錯誤可以有人幫助我?:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1443 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.". 

我試圖尋找,但我沒有」真的很瞭解。

從我的理解端口是不正確的,但我如何找到正確的IP /端口?

+0

從我的理解端口是不正確的,但我如何找到正確的IP /端口? –

+0

如果您有多個網絡接口,請嘗試使用這些IP地址。 – RaviH

+0

這應該是在你的問題,否則它可以被錯過。 – Batty

回答

1

1)打開SQL Server配置管理器,檢查是否啓用了網絡配置協議下的TCP/IP。

2)在「連接」下的SQL服務器的屬性下,檢查是否允許允許到此服務器的遠程連接。

3)檢查是否可以通過SSMS連接並查詢數據庫。

4)在SQL Server配置管理器中檢查SQL Server Browser服務是否正在運行。 (默認情況下這不會啓用,也不會默認啓動)。

5)如果所有這些設置,那麼我會檢查防火牆。

(對於任何人都可能會遇到這樣的解決方案是讓SQL Server和Windows身份驗證)

+0

啓用tcp/ip狀態 –

+0

服務器允許遠程連接.. –

+0

您有防火牆嗎?你也可以使用該用戶名/密碼通過SSMS連接嗎? –

0

確保您的SQL Server配置 都具有一個TCP配置,並在該IP地址配置。

您可以測試有兩件事情:

  1. 關閉Windows防火牆,從SERVICES.MSC。
  2. 嘗試通過IP地址,用戶名,pwd連接到SQL Server, 應用程序正在嘗試使用。 (打開企業管理器,連接,輸入IP地址等)
0

嘗試使用SQLServer的jtds驅動程序。

0

Here

並與你的代碼看起來像這樣安裝Oracle JDBC驅動程序:

import java.sql.DriverManager; 
import java.sql.Connection; 
import java.sql.SQLException; 

public class OracleJDBC { 

    public static void main(String[] argv) { 

     System.out.println("-------- Oracle JDBC Connection Testing ------"); 

     try { 

      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

     } catch (ClassNotFoundException e) { 

      System.out.println("Where is your Oracle JDBC Driver?"); 
      e.printStackTrace(); 
      return; 

     } 

     System.out.println("Oracle JDBC Driver Registered!"); 

     Connection connection = null; 

     try { 

      connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:BankSystem", "Andrew","andrei23"); 

      } 
     catch (SQLException e) { 

      System.out.println("Connection Failed! Check output console"); 
      e.printStackTrace(); 
      return; 

     } 

     if (connection != null) { 
      System.out.println("You made it, take control your database now!"); 
     } else { 
      System.out.println("Failed to make connection!"); 
     } 
    } 

} 

它可能應該工作,如果沒有,什麼是輸出樣子呢?仍然等於之前的情況?你檢查過防火牆嗎?連接到服務器允許?

+0

拋出java.lang.ClassNotFoundException訪問:com.microsoft.sqlserver.jdbc.SQLServerDriver 和我安裝ojdbc6.jar到buildpaths –

+0

會不會是你的JDBC驅動程序不你的項目的類路徑?你有沒有檢查jdbc的偏好,就像正確的路徑? – 2014-02-17 16:16:51

+0

在ojdbc6.jar中沒有com.microsoft ....只有oracle.jdbc.etc –

0

據我所知,sqlserver的標準端口是1433而不是1443。