在eclipse中连接到SQLserver

时间:2014-06-25 19:27:47

标签: java sql-server eclipse

我在使用Eclipse连接到Java中的SQLserver时遇到问题。我使用过SQLjdbc4,但还有问题。

我不知道我的代码有什么问题,但它不起作用。我搜索过类似的问题,但它没有用。

这是我的代码:

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 个答案:

答案 0 :(得分:0)

解决问题

1)如果这是一次性问题,请启用“sa”登录,并更改“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