Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 
like image 257
The Code Geek Avatar asked Jun 28 '13 19:06

The Code Geek


People also ask

How do I fix SQL Server login failed?

Check out Windows or Mixed authentication mode is enabled. For that, select the database, right click on it, and choose “Properties”. In the Security tab, check out the server authentication section. If the authentication mode is changed, then do not forget to restart SQL Server to allow changes to take place.

How do I fix user login failed?

Select Windows Authentication from the Authentication drop-down, then click Connect to log in to SQL Server. Right-click on the server name (ie COMPUTERNAME\ACCTIVATE) and choose Properties . Click Security in the list on the left. Select SQL Server and Windows Authentication mode on the right and click OK.

Can not connect to SQL Server Login failed for user?

The generic message “Login Failed for User (Microsoft SQL Server, Error: 18456)” means you entered invalid credentials when logging into SQL Server.

How do I fix the Login failed for user sa Microsoft SQL Server Error 18456?

Open the SQL Server as Administrator Click Windows and type SQL Server Management Studio. Then click Yes (if UAC prompt received) and check if the SQL server is clear of the error 18456. If not, then check if disabling UAC on the server machine solves the issue.


2 Answers

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

like image 165
Bryan Swan Avatar answered Sep 21 '22 02:09

Bryan Swan


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 the Ok button enter image description here

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

After that, I could log in with user and password

like image 21
Junior Grão Avatar answered Sep 20 '22 02:09

Junior Grão



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!