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

2023-10-15Java开发问题
7

本文介绍了接收 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13