SqlServer: Login failed for user

2020-01-23 21:07发布

I've written a very simple jdbc login test program. And after all kinds of problems I've almost got it working. Almost, just can't seem to get past this "SQLServerException: Login failed for user xxxxx" problem.

I created a simple database PersonInfo then I created user user1 password1 (sql authenication). And after trying everything was unable to connect to the database.

I am using SqlServer2008 on Win 7, I'v got the latest jdbc driver from Microsoft.

My code is:

import java.sql.*;

public class hell {
public static void main(String[] args) {

    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection conn=  DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=PersonInfo;user=Sohaib;password=0000;");


System.out.println("connected");
       }

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

Here's the Exception

Exception: Unable to get connect
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'Sohaib'.
and all other supporting errors..

Unfortunately I am now dead in the water until some kind SqlServer Guru or Jdbc Guru take pity on me and helps me out.

thanks in advance.

9条回答
叼着烟拽天下
2楼-- · 2020-01-23 21:16

For Can not connect to the SQL Server. The original error is: Login failed for user 'username'. error, port requirements on MSSQL server side need to be fulfilled.

There are other ports beyond default port 1433 needed to be configured on Windows Firewall.

https://stackoverflow.com/a/25147251/1608670

查看更多
Anthone
3楼-- · 2020-01-23 21:18

Is your SQL Server in 'mixed mode authentication' ? This is necessary to login with a SQL server account instead of a Windows login.

You can verify this by checking the properties of the server and then SECURITY, it should be in 'SQL Server and Windows Authentication Mode'

This problem occurs if the user tries to log in with credentials that cannot be validated. This problem can occur in the following scenarios:

Scenario 1: The login may be a SQL Server login but the server only accepts Windows Authentication.

Scenario 2: You are trying to connect by using SQL Server Authentication but the login used does not exist on SQL Server.

Scenario 3: The login may use Windows Authentication but the login is an unrecognized Windows principal. An unrecognized Windows principal means that Windows can't verify the login. This might be because the Windows login is from an untrusted domain.

It's also possible the user put in incorrect information.

http://support.microsoft.com/kb/555332

查看更多
我想做一个坏孩纸
4楼-- · 2020-01-23 21:18

I ran into the same issue, and I fixed it by adding my windows username to SQL and then to my server, here is how I did:

First, create a new login with your Windows username: enter image description here

Click Search, then type your name in the box and click check names.. enter image description here

Then add your that user to the server:

Right click on the Server > Permissions > Search > Browse > Select your user (You will notice that now the user you created is available in the list)

enter image description here

I hope it helps ;-)

查看更多
Juvenile、少年°
5楼-- · 2020-01-23 21:20

I got the same error message when trying to connect to my SQL DB in Azure (using sql-cli). The simple solution was to escape the password with single quotes like this:

mssql -s server.database.windows.net -u user@server -p 'your_password' -d your_db -e
查看更多
贼婆χ
6楼-- · 2020-01-23 21:21

We got this error when reusing the ConnectionString from EntityFramework connection. We have Username and Password in the connection string but didn't specify "Persist Security Info=True". Thus, the credentials were removed from the connection string after establishing the connection (so we reused an incomplete connection string). Of course we always have to think twice when using this setting, but in this particular case it was ok.

查看更多
劳资没心,怎么记你
7楼-- · 2020-01-23 21:23

We solved our Linux/php hook to SQL Server problem by creating a new login account with SQL Server authentication instead of Windows authentication.

查看更多
登录 后发表回答