0

I am getting this error message in Java while trying to connect to a SQL Server database:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'admin'. ClientConnectionId:81b24c93-1297-4713-91c8-7f639b172de3
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
    at first.assign_Code.createConn(assign_Code.java:34)
    at first.assign_Code.getQueryResultInMap(assign_Code.java:42)
    at first.assign_Code.main(assign_Code.java:73)

It is strange because it can establish connection, but fails trying to login.

This is my code:

 package first;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

public class assign_Code {
    static Connection conn = null;
    static QueryRunner run = new QueryRunner();
    static boolean keepConnection = false;
    
    public static void createConn(String urlString, String driverr, String usernameString, String password) throws SQLException, IOException {

        if (conn == null || conn.isClosed()) {
            String driver = null;
            DbUtils.loadDriver(driver);
            conn = DriverManager.getConnection(urlString, usernameString, driverr);
            conn.setAutoCommit(false);
        }
    }
    
    public static Map<String, Object> getQueryResultInMap(String urlString, String driverr, String usernameString, String password,String sqlQuery, Object... params) throws SQLException, IOException {

        try {
            createConn(urlString,driverr,usernameString,password);
            if (params == null) {
                return run.query(conn, sqlQuery, new MapHandler());
            } else {
                return run.query(conn, sqlQuery, new MapHandler(), params);
            }
        } catch (SQLException se) {
            se.printStackTrace();
            return null;
        } finally {
            closeConn();
        }
    }
    
    public static void closeConn() throws SQLException {
        if (!keepConnection) {
            DbUtils.closeQuietly(conn);
        }
    }
    
    
    public static void main(String[] args) throws InterruptedException, SQLException, IOException{
    System.setProperty("webdriver.chrome.driver","C:\\Users\\Steve\\Desktop\\SELENIUM\\chromedriver.exe");
    
    String urlString="jdbc:sqlserver://dfd45.hhgt.local:1433;databaseName=Test";
    String usernameString="admin";
    String password="admin";
    String driverr="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String sqlQuery= "select sku from fuse.orders.WebOrderItem where itemid = ?";
    
    
    Map<String,Object>resultSet= getQueryResultInMap(urlString, driverr, usernameString, password, sqlQuery, "3");

    WebDriver driver = new ChromeDriver();
    
    }

}

Take a look at this:

    String urlString="jdbc:sqlserver://dfd45.hhgt.local:1433;databaseName=Test";
    String usernameString="admin";
    String password="admin";
    String driverr="com.microsoft.sqlserver.jdbc.SQLServerDriver";

Am I doing anything wrong? I can connect to the SQL Server using SQL Server Management Studio, but it fails to connect in Java:

enter image description here

What's wrong here? Is anything wrong?

1
  • "Login failed" doesn't necessarily mean "Incorrect username or password," though that's often the case. When specifying the databaseName parameter in your JDBC connection string the target database has to exist already and your SQL Login must have previously been granted access to it. Does the Test database already exist? Has the admin login been mapped to a database user with sufficient access in it? Given the select query perhaps you need to specify the fuse database instead? Commented Oct 3, 2022 at 5:50

2 Answers 2

2

You did not include the password in the statement:

conn = DriverManager.getConnection(urlString, usernameString, driverr);

It should be:

conn = DriverManager.getConnection(urlString, usernameString, password);
Sign up to request clarification or add additional context in comments.

2 Comments

It should be DriverManager.getConnection(urlString, usernameString, password), the driverr doesn't belong in the request for a connection.
sorry, didn't notice that, thanks
2

As I see:

conn = DriverManager.getConnection(urlString, usernameString, driverr);

This string is wrong.

This method describes:
public static Connection getConnection(String url,
        String user, String password) throws SQLException

At the 3rd place must be the password.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.