2014-06-25 61 views
0

我在使用Eclipse連接到Java中的SQLserver時出現問題。我已經使用SQLjdbc4,但仍然有問題。在eclipse中連接到SQLserver

我不知道我的代碼有什麼問題,但它不起作用。我搜索了類似的問題,但沒有用。

這是我的代碼:

import java.*; 
import java.sql.*; 
public class Connect{ 
    public static void main(String[] args) { 

      // Create a variable for the connection string. 
      String connectionUrl = "jdbc:sqlserver://qp-vaio\\qp:1433;" + 
      "databaseName=db;user=sa;password=1"; 

      // 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) {} 
      } 
     } 
} 

這就是問題所在:

Login failed for user 'sa'. ClientConnectionId:f87b859a-c901-4ce4-8e51-02478ffac83d 
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216) 
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254) 
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220) 
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827) 
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at java.sql.DriverManager.getConnection(Unknown Source) 
    at Connect.main(Connect.java:18) 
+1

什麼問題?發佈異常堆棧跟蹤... – Nivas

+0

你得到了什麼輸出或異常? –

回答

0

工作圍繞該問題

1)如果這是一個時間的問題,使「sa」login with更改「sa」登錄密碼。

ALTER LOGIN sa WITH PASSWORD = 'yourpass' UNLOCK ; 
GO 

2)如果這是一次性問題,請在不更改密碼「sa」登錄的情況下啓用「sa」登錄。

ALTER LOGIN sa WITH CHECK_POLICY = OFF; 
ALTER LOGIN sa WITH CHECK_POLICY = ON; 
GO