Java connection to SQL Server database: Login failed for user '

I am trying to connect to a database that I created using SQL Server 2012, but I get an error all the time. This is the code to connect:

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 I get:

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

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

EDIT . This is the whole stack.

 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) 
+4
source share
6 answers

Invalid URL for this database.

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

try it instead

 Connection connection = DriverManager .getConnection("jdbc:sqlserver://localhost:1433;\\SQLEXPRESS;databaseName=Tema6","sa","123456"); 
0
source

Click SQL -> right-click -> propretise -> Securiti -> mark SQL Server and Windows Authentication Mode

90% success!

+3
source

I did not check the stack trace, but very often people forget about the possibility of remote connection to the server. Try right-clicking on the server instance (Object Explorer)> "Properties"> "Connections" and check Allow remote connections to this server

+2
source

I do not think that this is a problem with the driver, as the message indicates that it tried and did not authenticate, so at least I managed to connect.

The first (main) question I would ask is, are you sure that β€œ123456” is the correct password for the sa account? Test it by logging into SQL Server Management Studio as "sa".

Next, I will try to create a user in SQL Server and provide these credentials in the connection string (as suggested by Brandon)

Next (for fun) I would enable authentication in mixed mode and try to use the credentials of the Windows user account in the connection string.

Here are some instructions that I wrote about setting up a network in SQL Server in the past. It’s definitely worth checking the settings twice:

SQL Server Network Setup
Based on this, SQL Server 2008 Express does not support TCP connections on a fixed port. To solve this problem: In 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 the value to YES On the IP Addresses tab "scroll down In the" IP All "section, change the TCP port to 1433, make sure that the dynamic TCP ports are empty, delete zero Click Apply

In SQL Server Management Studio, verify: From the server instance, right-click and select properties. In the Security section: Verify that SQL Server and Windows Authentication is installed.

+2
source

Check the port on which SQL Server is configured. You get this strange error if the port is configured as a "dynamic port" and you are trying to connect to the default port.

You can check if this is a problem or not by trying to connect to sql server through sqlserver client.

  • Set Authentication Mode to "SQL Server Authentication"
  • Enter the server name and port in the format "server name, port" in the server field
  • Enter your username and password and click

If the connection failed and you get the exact error message "login failed for user xxx", then this is the problem

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

+1
source

Go to sql server, select user properties and change server roles so that sysadmin solves the problem.

0
source

Source: https://habr.com/ru/post/1483983/


All Articles