Search code examples
sql-serversql-server-2008jdbc

SqlServer: Login failed for user


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 problem:

SQLServerException: Login failed for user xxxxx

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

I am using SqlServer2008 on Win 7, I've 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.

Solution

  • 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 ;-)