4

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)
vidit
  • 6,293
  • 3
  • 32
  • 50
Daniel Pop
  • 43
  • 1
  • 1
  • 4
  • Please post the whole stacktrace of the connection error to analyse the possible problems. – Luiggi Mendoza Jun 01 '13 at 22:23
  • 1
    Really strange, the stacktrace doesn't give enough info to detect the error. Try using another JDBC driver to connect to your SQL Server database engine and check the errors there. I recommend to use [jTDS](http://jtds.sourceforge.net/). – Luiggi Mendoza Jun 01 '13 at 22:31
  • What if you add integratedSecurity=false; to the connection string? – Brandon Jun 01 '13 at 22:41
  • 1
    @Brandon I still get the same error. – Daniel Pop Jun 01 '13 at 22:46
  • @LuiggiMendoza I replaced the driver with the one you linked and it still does not work. The error message is pretty much the same. – Daniel Pop Jun 01 '13 at 22:48
  • Then there must be a problem with your SQL Server engine. Ask for support on its page. – Luiggi Mendoza Jun 01 '13 at 22:49
  • Try creating a new user with a specific password and grant the sysadmin role. That will tell you if SQL Server is messed up or something about the 'sa' account. – Brandon Jun 01 '13 at 22:55
  • Which user is the owner of the database that you're trying to connect to? If user "sa" doesn't have the correct permissions on that database, it could cause the login problem you're having. – luther07 Jun 01 '13 at 22:56
  • This error i've seen in some time, the solutions may be different but you should first check the following: 1. Open SQL Server Management Studio and check 'sa' permissions. 2. Download the latest version of JDBC driver (http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774) 3. Remove :1433 PORT from DB_URL, in some cases the port is not necessary, and sometimes causes errors. 4. Should be: String DB_URL = "jdbc:sqlserver://localhost;databaseName=Tema6;user=sa;password=123456"; or try with 127.0.0.1 – Gaston Flores Jun 01 '13 at 23:20
  • @GastonF. I tried many combinations for the url String, but nothing helped. Thanks, anyway. – Daniel Pop Jun 02 '13 at 08:35

6 Answers6

6

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

Simon Curd
  • 790
  • 5
  • 12
  • 1. Yes, I double checked that password and it's correct. 2. I created another user and I granted the syasdmin role. Still, the same outcome. 3. I've tried using windows authentication, but, again, same output As far as configuring SQL Server Networking, I did all that you were telling me even before I tried to connect to DB, and it's all good. The rest of the settings are the default ones, I didn't modify anything else, that's why I find it so weird. – Daniel Pop Jun 02 '13 at 08:33
  • Yeah sounds very odd. Scraping the barrel here: 1. Does the error change if you remove the database reference from the connection string? 2. Try using 127.0.0.1 as the IP address 3. Try using the actual network ip instead of localhost 4. Try rebooting 5. Try reinstalling! That's it I'm fresh out of ideas – Simon Curd Jun 02 '13 at 09:49
  • Ok so, finally, I gave up on SQL Server, I installed MySQL Workbench and made a server(I hope this is the right term) using XAMPP. This time, it all worked out and I can continue now with my Java app. Thank you all for your readiness. – Daniel Pop Jun 03 '13 at 15:38
3

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

90% success!

iuiuadu_93
  • 45
  • 5
1

I haven't really checked out your stack trace but it is very common for people to forget about allowing remote connections to server. Try right-click on server instance (Object Explorer) > Properties > Connections and check Allow remote connections to this server

1

This database url is in wrong format

String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=Tema6;user=sa;password=123456";

instead of this try this

Connection connection = DriverManager
                .getConnection("jdbc:sqlserver://localhost:1433;\\SQLEXPRESS;databaseName=Tema6","sa","123456");
MIkka Marmik
  • 1,101
  • 11
  • 29
1

Check the port on which sql server is configured. You get this weird error if the port is configured as "dynamic port" and you are trying to connect on the default port.

You could check if this is the issue or not by trying to connect to the sql server through sqlserver client.

  1. Set the authentication mode to "sql server authentication"
  2. Enter the servername and port in the format "servername,port" in the server field
  3. Enter user name and password and click connect

If the connect didnt succeed and you get the exact error message "login failed for user xxx" then this is the issue

https://msdn.microsoft.com/en-IN/library/ms177440.aspx

  • Saved the day! I was specifying the jdbc port and wasted hours of time on this with the message of login failed. Removed the port from the connection and boom all fixed – Paul Zepernick Apr 18 '18 at 13:30
-1

Go to sql server, select user properties and change server roles to sysadmin solved the issue.

  • Assigning a sysadmin role to regular database users that clients use without a very specific requirement is generally not best practice and should be avoided. – Parth Patel Feb 14 '22 at 16:16