Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java connection to a SQL Server Database: Login failed for user 'sa' [duplicate]

I am trying to connect to a database that I created using SQL Server 2012, but I keep getting an error. This is the code for the connection:

Driver d = (Driver)Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=Tema6;user=sa;password=123456";
        java.sql.Connection con = DriverManager.getConnection(DB_URL);

And this is the error that I am getting:

Login failed for user 'sa'. ClientConnectionId:e6335e64-ca68-4d72-8939-5b7ded951424

I have enabled TCP/IP protocol from SQL Server Config, I am sure that the 'sa' account is enabled and that the password is correct. Can anyone help me, please?

EDIT: This is the entire stacktrace.

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:e6335e64-ca68-4d72-8939-5b7ded951424
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.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Connection.main(Connection.java:12)

EDIT2: After replacing the driver with jTDS:

java.sql.SQLException: Login failed for user 'sa'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2893)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2335)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:609)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:369)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:183)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Connection.main(Connection.java:19)
like image 402
Daniel Pop Avatar asked Dec 20 '22 04:12

Daniel Pop


2 Answers

I don't think it's a driver issue, since the message indicates that it's tried and failed authentication, so at least managed to connect.

First (basic) question I'd ask is whether you're sure '123456' is the right password for the sa account? Test it by logging in with SQL Server Management Studio as 'sa'.

Next I'd try creating a user in SQL Server and supplying those credentials in the connection string (as suggested by Brandon)

Next (for fun) I'd enable mixed-mode authentication and try using my windows user account credentials in the connection string.

These are some instructions I wrote for configuring the networking in SQL Server in the past. Definitely worth double-checking the settings:

Configuring SQL Server Networking
Out of the box, SQL Server 2008 Express does not support TCP connections on a fixed port. To resolve this: From the windows start menu open "SQL Server Configuration Manager" Under "SQL Server Network Configuration" --> "Protocols for SQLEXPRESS" open the TCP/IP properties On the "Protocol" tab set enabled to YES On tab "IP Addresses" scroll to the bottom Under "IP All" change the TCP Port to 1433 ensure the TCP Dynamic Ports is empty, delete the zero Click apply

In SQL Server Management Studio check: From server instance right click and select properties Under the Security option : ensure that "SQL Server and Windows Authentication mode" is checked

like image 104
Simon Curd Avatar answered May 14 '23 02:05

Simon Curd


Click SQL --> click right mouse --> propretise --> Securiti -->tick SQL server and windows Authentication mode

90% success!

like image 20
iuiuadu_93 Avatar answered May 14 '23 02:05

iuiuadu_93