Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (4k points)
closed by

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 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.

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

closed

4 Answers

0 votes
by (7.8k points)
 
Best answer
You are encountering a "SQLServerException: Login failed for user xxxxx" problem when trying to establish a JDBC connection to your SQL Server database. Despite creating the database and user, and using the correct JDBC driver from Microsoft, you are unable to connect. The provided code snippet shows the connection setup using DriverManager.getConnection(). To troubleshoot, ensure the correctness of the database name, username, and password, check if the SQL Server is running and accessible, verify user permissions, review any firewall or network configurations, and consider updating the JDBC driver. Further assistance can be sought from the official documentation or developer community.
0 votes
by (8.7k points)
edited by

This issue can be solved by adding your Windows username to SQL and then to your Server.

Kindly follow the below steps to get rid of this problem:

Step1: Open the Microsoft SQL server management studio and create new login by clicking on the login tab using your window login username.

Step2: select the search tab and type your name under the enter object name to select and after that hit the check name button.

Step3:Now add that username to the server by Right click to server->select permission->click search and browse->Select the user from the list that popped up.

And finally, you are done with the solution.

Also, enroll in our Online SQL Certification to learn the basic concepts of Microsoft SQL Server!

0 votes
by (13k points)

The issue you are facing is related to the "SQLServerException: Login failed for user xxxxx" error when attempting to connect to your SQL Server database using JDBC.

It seems that you have created a simple database called "PersonInfo" and a user named "user1" with the password "password1" for SQL authentication.

However, despite trying various solutions, you are still unable to establish a connection to the database.

You mentioned using SQL Server 2008 on Windows 7 and having the latest JDBC driver from Microsoft.

Looking at your code snippet, it appears that you have imported the necessary classes and attempted to establish a connection using the DriverManager.getConnection() method.

The connection URL you provided includes the hostname ("localhost"), port number ("1433"), database name ("PersonInfo"), username ("Sohaib"), and password ("0000").

The error message you received indicates that the login failed for the user "Sohaib," along with other supporting errors.

To troubleshoot this issue, you can try the following steps:

Double-check the correctness of the database name, username, and password.

Verify that the SQL Server is running and accessible on the specified hostname and port.

Ensure that the user "Sohaib" has the necessary permissions to connect to the "PersonInfo" database.

Check if any firewall or network configurations are blocking the connection.

Consider updating the JDBC driver or checking for any known compatibility issues between the driver and SQL Server version.

If the problem persists, it might be helpful to consult the official documentation or seek assistance from the developer community to resolve the login issue and successfully establish the JDBC connection.

0 votes
by (11.4k points)
The issue is a "SQLServerException: Login failed for user xxxxx" error when connecting to the SQL Server database using JDBC. Troubleshoot by verifying the database name, username, and password, checking server accessibility, reviewing user permissions, and updating the JDBC driver if necessary.

Browse Categories

...