2010-04-09 18 views
3
連接到SQL Server 2008

我想2008年服務器從Java從Java

這裏連接到SQL Server是一個程序

import java.sql.*; 

public class connectURL { 

    public static void main(String[] args) { 

     // Create a variable for the connection string. 
     String connectionUrl = "jdbc:sqlserver://localhost/SQLEXPRESS/Databases/HelloWorld:1433;";// + 
      //"databaseName=HelloWorld;integratedSecurity=true;"; 

     // Declare the JDBC objects. 
     Connection con = null; 
     Statement stmt = null; 
     ResultSet rs = null; 

      try { 
       // Establish the connection. 
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
        con = DriverManager.getConnection(connectionUrl); 

        // Create and execute an SQL statement that returns some data. 
        String SQL = "SELECT TOP 10 * FROM Person.Contact"; 
        stmt = con.createStatement(); 
        rs = stmt.executeQuery(SQL); 

        // Iterate through the data in the result set and display it. 
        while (rs.next()) { 
         System.out.println(rs.getString(4) + " " + rs.getString(6)); 
        } 
      } 

     // Handle any errors that may have occurred. 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 

     finally { 
      if (rs != null) try { rs.close(); } catch(Exception e) {} 
       if (stmt != null) try { stmt.close(); } catch(Exception e) {} 
       if (con != null) try { con.close(); } catch(Exception e) {} 
     } 
    } 
} 

但它顯示誤差

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/SQLEXPRESS/Databases/HelloWorld, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server 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:170) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716) 
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at connectURL.main(connectURL.java:43) 

我給出瞭如下所示的所有說明:http://teamtutorials.com/database-tutorials/configuring-and-creating-a-database-in-ms-sql-2008

可能是什麼問題?

+1

您是否能夠通過SQL Server的客戶端工具進行連接? – 2010-04-09 10:34:23

回答

9

我連接的URL可能是錯的。然後嘗試以下連接,

String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=YourDBName;user=UserName;Password=YourPassword" 
2

您是否在您的hosts文件中定義了localhost?嘗試用連接URL中的127.0.0.1替換localhost。

...而且你有SQL Server在同一臺計算機上運行,​​對不對?

2

當您安裝Express版本的SQL Server時,默認情況下將禁用TCP/IP連接。您需要運行SQL Server配置管理器並打開TCP/IP。您還需要將它正在偵聽的端口設置爲1433.