我正在尝试通过 Java 连接到 SQL Server 2008。
- 我已将
sqljdbc4.jar
添加到我的项目库中。 - 没有为数据库访问数据库(Windows 身份验证)设置用户名和密码。
- 1433端口正在监听,但还是收到这个异常:
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:
jdbc:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true
您还需要 sqljdbc_auth.dll
(注意 32/64 位)在您的 Windows 系统路径或通过 java.library.path
定义的目录中
有关详细信息,请参阅驱动程序手册:http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated
关于java - 接收 SQLException "Login failed for user"连接到 SQL Server 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14945075/