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.(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)
解决方案
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");