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条回答
一纸荒年 Trace。
2楼-- · 2020-01-23 21:30

In my case, I had to activate the option "SQL Server and Windows Authentication mode", follow all steps below:

1 - Right-click on your server enter image description here

2 - Go to option Security

3 - Check the option "SQL Server and Windows Authentication mode"

4 - Click on Ok button enter image description here

5 - Restart your SQL Express Service ("Windows Key" on keyboard and write "Services", and then Enter key) enter image description here

After that, I could login with user and password

查看更多
放荡不羁爱自由
3楼-- · 2020-01-23 21:31

Also make sure that account is not locked out in user properties "Status" tab

查看更多
Fickle 薄情
4楼-- · 2020-01-23 21:38

If you are using Windows Authentication, make sure to log-in to Windows at least once with that user.

查看更多
登录 后发表回答