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

Sal*_*ara 7 java connection-string sqlexception sql-server-2008-r2

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

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

SQL异常: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());
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 15

如果需要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