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

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

  1. 我已经将sqljdbc4.jar添加到我的项目库中。
  2. 没有为访问数据库的数据库设置用户名和密码(Windows身份validation)。
  3. 1433端口是Listening,但我仍然收到此exception:

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

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

 JDBC:SQLSERVER://本地主机:1433;的databaseName = DB; integratedSecurity =真

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

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

当我尝试从Java连接到Microsoft SQL服务器时,我遇到了同样的问题。 我使用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); 

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

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

我希望它运作良好!

这篇文章我的帮助: jdbc.SQLServerException:任何用户的用户登录失败

您需要integratedSecurity=true标志,并确保在安全性下确实将服务器属性设置为“Windows身份validation模式”。

Interesting Posts