通过Java代码连接到MicroSoft Sql Server 2012时出现连接错误

时间:2015-07-23 07:46:22

标签: java sql-server

我正在尝试通过以下java代码连接到Ms Sql server 2012。

`public class MsSqlServerConnection {

public static void main(String[] args) throws Exception{

    try {

        Driver d = (Driver)Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    /*  String connectionURL = "jdbc:sqlserver://localhost:1433;databaseName=Gpstrack;user=;password="; */
        Connection connection = null;
        /*Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");*/
        connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;user=BARCELONA\\V&V;database=Gpstrack");

        if(connection!=null){
            System.out.println("connection is successfull");
        }else
        {
            System.out.println("Connection error");
        }

    } catch (SQLException e) {
        throw e;
    } catch (Exception e) {
        throw e;
    }

}

}`

当我运行上面的代码时,我收到以下错误。

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'BARCELONA\V&V'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2529)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1905)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1893)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1045)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MsSqlServerConnection.main(MsSqlServerConnection.java:17)

所以请帮我摆脱这个错误。

1 个答案:

答案 0 :(得分:0)

您的活动目录域用户密码为空?这是相当不寻常的,有点危险,但我只是试着关注你的问题。

' BARCELONA \'到sql server表示域名,我没有看到你的连接字符串表示集成安全性。

因此,它可以像在连接字符串中指示集成安全性一样简单:jdbc:sqlserver://localhost:1433;integratedSecurity=true;

请参阅this link for documentation on building your connection string url for jdbc