接收SQLException“用户登录失败”连接到SQL Server 2008 [英] Receiving SQLException "Login failed for user" connecting to SQL Server 2008

查看:241
本文介绍了接收SQLException“用户登录失败”连接到SQL Server 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Java连接到SQL Server 2008.

I am trying to connect to SQL Server 2008 via Java.


  1. 我添加了 sqljdbc4.jar 到我项目的库。

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

  3. 1433端口是Listening,但我仍然收到此异常:

  1. I've added sqljdbc4.jar to my project's library.
  2. No username and password is set for database accessing the database (Windows Authentication).
  3. The 1433 port is Listening, but I still receive this exception:




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

SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. 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());
        }
     }
}


推荐答案

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

If you want windows authentication you need to add the option integratedSecurity=true to your JDBC URL:


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

您还需要Windows系统路径或通过 java.library.path sqljdbc_auth.dll (小心32/64位) $ c>

You also need sqljdbc_auth.dll (beware of 32/64 bit) in your Windows system path or in a directory defined through java.library.path

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

For details see the driver's manual: http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated

这篇关于接收SQLException“用户登录失败”连接到SQL Server 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆