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

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

问题描述

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

  1. 我已将 sqljdbc4.jar 添加到我的项目库中.
  2. 没有为访问数据库的数据库设置用户名和密码(Windows 身份验证).
  3. 1433 端口正在侦听,但我仍然收到此异常:

<块引用>

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

相关代码:

公共类数据库{私人连接链接;私有 java.sql.Statement stmt;公共结果集rs;公共数据库(){尝试{Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB;";连接 con = DriverManager.getConnection(connectionUrl);}捕获(SQLException e){System.out.println("SQL 异常:"+ e.toString());}捕获(ClassNotFoundException cE){System.out.println("找不到类异常:"+ cE.toString());}}}

解决方案

如果您想要 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

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

  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 Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. ClientConnectionId:085d5df3-ad69-49e1-ba32-b2b990c16a69

Relevant code:

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());
        }
     }
}

解决方案

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

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

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

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

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