与SQL Server数据库的Java连接:用户'sa'的登录失败 [英] Java connection to a SQL Server Database: Login failed for user 'sa'
问题描述
我正在尝试连接到我使用SQL Server 2012创建的数据库,但我一直收到错误。这是连接的代码:
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);
这是我得到的错误:
Login failed for user 'sa'. ClientConnectionId:e6335e64-ca68-4d72-8939-5b7ded951424
我已从SQL Server启用TCP / IP协议配置,我确信'sa'帐户已启用且密码正确。有人可以帮帮我吗?
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:用jTDS替换驱动程序后:
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)
推荐答案
此数据库网址格式错误
字符串DB_URL =jdbc:sqlserver:// localhost:1433; databaseName = Tema6; user = sa; password = 123456;
String DB_URL = "jdbc:sqlserver://localhost:1433;databaseName=Tema6;user=sa;password=123456";
而不是这个试试这个
Connection connection = DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;\\SQLEXPRESS;databaseName=Tema6","sa","123456");
这篇关于与SQL Server数据库的Java连接:用户'sa'的登录失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!