首页 文章

接收连接到SQL Server 2008的SQLException“用户登录失败”

提问于
浏览
7

我试图通过Java连接到SQL Server 2008 .

  • 我已将 sqljdbc4.jar 添加到我的项目库中 .

  • 没有为访问数据库的数据库设置用户名和密码(Windows身份验证) .

  • 1433端口正在侦听,但我仍然收到此异常:

SQL异常:com.microsoft.sqlserver.jdbc.SQLServerException:用户''登录失败 . ClientConnectionId:085d5df3-ad69-49e1-ba32-b2b990c16a69

相关代码:

public class DataBases 
{

    private  Connection link;
    private  java.sql.Statement  stmt;
    public    ResultSet rs;

    public DataBases() 
    {  
        try 
        {    
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB;";
            Connection con = DriverManager.getConnection(connectionUrl);
        } 
        catch (SQLException e) 
        {
            System.out.println("SQL Exception: "+ e.toString());
        }
        catch (ClassNotFoundException cE)
        {
            System.out.println("Class Not Found Exception: "+ cE.toString());
        }
     }
}

4 回答

  • 0

    如果需要Windows身份验证,则需要将选项 integratedSecurity=true 添加到JDBC URL:

    jdbc:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true
    

    您还需要在Windows系统路径中或通过 java.library.path 定义的目录中 sqljdbc_auth.dll (小心32/64位)

    有关详细信息,请参阅驱动程序手册:http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated

  • 1

    当我尝试从Java连接到Microsoft SQL Server时,我遇到了同样的问题 . 我使用jTDS驱动程序而不是常规 SQLJdbdc 驱动程序 .

    Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
            String connectionUrl = "jdbc:jtds:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true";
            Connection con = DriverManager.getConnection(connectionUrl);
    
  • 1

    我遇到过同样的问题 . 这是因为ConnectionUrl格式错误 . 您在ConnectionUrl中缺少用户名和密码 .

    String ConnectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB","username","password");"
    

    我希望它运作良好!

  • 15

    这篇文章我的帮助:jdbc.SQLServerException: Login failed for user for any user

    您需要 integratedSecurity=true 标志并确保服务器属性确实设置为安全性下的'Windows Authentication Mode' .

相关问题