简体   繁体   English

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

[英]Connection error while Connecting to MicroSoft Sql Server 2012 through Java Code

I am trying to connect to Ms Sql server 2012 through following java code. 我正在尝试通过以下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;
    }

}

}` }`

When I run the above code I am getting the following error. 当我运行上面的代码时,出现以下错误。

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)

So please help me to get rid of this error. 因此,请帮助我摆脱这个错误。

You have an active directory domain user with a blank password? 您有一个活动目录域用户,密码为空? That's fairly unusual and somewhat dangerous, but I'll just try to focus on your question. 这是非常不寻常的,而且有些危险,但是我将尝试着重于您的问题。

'BARCELONA\\' to sql server indicates a domain name, and I do not see your connection string indicating integrated security. sql server的“ BARCELONA \\”表示域名,我看不到您的连接字符串表示集成安全性。

So it could be something as simple as indicating integrated security in your connection string: jdbc:sqlserver://localhost:1433;integratedSecurity=true; 因此,就像在连接字符串中指示集成安全性一样简单: jdbc:sqlserver://localhost:1433;integratedSecurity=true;

See this link for documentation on building your connection string url for jdbc . 有关为jdbc构建连接字符串url的文档,请参见此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM