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:
What's wrong here? Is anything wrong?

databaseNameparameter 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 theTestdatabase already exist? Has theadminlogin been mapped to a database user with sufficient access in it? Given the select query perhaps you need to specify thefusedatabase instead?