问题描述
我正在尝试通过 Java 连接到 SQL Server 2008.
- 我已将
sqljdbc4.jar
添加到我的项目库中. - 没有为访问数据库的数据库设置用户名和密码(Windows 身份验证).
- 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:
您还需要在您的 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.
- I've added
sqljdbc4.jar
to my project's library. - No username and password is set for database accessing the database (Windows Authentication).
- 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!