Unable to connect SQL express DB instance via jdbc when database password contains back slash

Shekar 21 Reputation points
2022-09-30T11:17:19.933+00:00

I am trying to connect to SQL express DB instance via jdbc. DB password is "Passw0rd\".

Here is the code I am using:

String url ="jdbc:sqlserver://hostname:2638;instanceName=SQLEXPRESSSYMC;integratedSecurity=false;encrypt=true;trustServerCertificate=true";  
String password = "Passw0rd\\";  
try  
{  
    System.out.println("url:" +url );  
    System.out.println("Password:"+password);  
    Connection conn = DriverManager.getConnection(url , "DBA", password);  
    System.out.println("connected");  

}  
catch (Exception e)  
{  
    e.printStackTrace();  
}   

I get following exception:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'DBA'. ClientConnectionId:3be3d544-73d3-4710-aa4e-dfe78b6dcbb1
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:262),
com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:283),
com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:129),
com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:37),
com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:5233),
com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3988),
com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3932),
com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7375),
com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3206),
com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2713),
com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2362),
com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213),
com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276),
com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861),
java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677),
java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)

If I set DB password which uses back slash character but not as an end character then it works. e.g
String password = "Passw0rd\a";

Please let me know how to resolve this?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,367 questions
0 comments No comments
{count} votes

Accepted answer
  1. YufeiShao-msft 7,091 Reputation points
    2022-10-03T08:02:58.02+00:00

    Hi @Shekar ,

    Some special character like bask slash, can be used as escape characters, this need to be validated in case it is interpreted as an escape character, so when using it, it often leads to errors, it needs to ensure that they are validated as string literals and not escape characters, for simple, it is recommended not to use bask slash

    -------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Olaf Helper 43,246 Reputation points
    2022-09-30T13:20:34.177+00:00

    It's known that some special characters in passwords or more precise in the complete connection string for data provider like ODBC or JDBC causes issues or failures.
    See for example
    https://community.microstrategy.com/s/article/Special-character-restrictions-of-username-and-password-in-ODBC-connection-for-Diamond-and-Platinum-gateways-new?language=en_US
    https://github.com/prisma/prisma/issues/11276
    https://www.oradba.ch/wordpress/2014/06/oracle-passwords-and-special-characters/

    Please let me know how to resolve this?

    By changing the password to an other strong one; one without backslash.

    1 person found this answer helpful.
    0 comments No comments