Java com.microsoft.sqlserver.jdbc.SQLServerException:用户“sa”登录失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23303687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:47:21  来源:igfitidea点击:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'

javasql-serverjdbc

提问by rocky

I am simply trying to test my connection with SQL Server 2008 as follows:

我只是想测试我与 SQL Server 2008 的连接,如下所示:

public class ForJDBC {


    public static void main(String[] args) {




        try{

        Connection conn = DriverManager.getConnection("jdbc:sqlserver://XX.XX.X.XXX:1433;databaseName=MyDB;user=sa;password=mypassword");

        if(conn != null){

            System.out.println("Connection is Successfull");
        }

        }
        catch(SQLException ex){

           ex.printStackTrace();
        }




    }
}

But I keep on getting the following exception:

但我不断收到以下异常:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:96f7d048-9734-4fd8-8216-21231d89b2e6
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    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
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
0(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(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:270) at forjdbc.ForJDBC.main(ForJDBC.java:35)

When I connect to MySQL 5.5, it works fine.

当我连接到 MySQL 5.5 时,它工作正常。

Troubleshooting Steps I have followed:

我遵循的故障排除步骤:

1) Tried Logging with the "sa" user name and password within the SQL Server Management Console using SQL Server Management studio

1) 尝试使用 SQL Server Management Studio 在 SQL Server 管理控制台中使用“sa”用户名和密码进行日志记录

2) I have sqljdbc4.jar in my Source Package folder of Netbeans 7.4

2) 我在 Netbeans 7.4 的 Source Package 文件夹中有 sqljdbc4.jar

3) Updated to Java 8

3) 更新到 Java 8

Please let me know what am I doing wrong and let me know if I can answer any questions.

请让我知道我做错了什么,如果我能回答任何问题,请告诉我。

回答by MIkka Marmik

Connection String Problem...

连接字符串问题...

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

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

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

回答by SparkOn

I think you missed loading the Driver class and registering it.

我认为您错过了加载 Driver 类并注册它。

##代码##

If still it is not working then please ensure your SQLServer services is running

如果仍然无法正常工作,请确保您的 SQLServer 服务正在运行

回答by iuiuadu_93

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

点击SQL --> 点击鼠标右键 --> propretise --> Securiti -->勾选SQL server and windows Authentication mode

90% success!

90%成功!