Receiving SQLException “Login failed for user” connecting to SQL Server 2008

ε祈祈猫儿з 提交于 2019-11-28 03:39:25

问题


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

回答1:


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




回答2:


I was having same issue when I tried to connect to Microsoft SQL server from Java. I used jTDS driver instead of regular SQLJdbdc Driver.

        Class.forName("net.sourceforge.jtds.jdbc.Driver"); 
        String connectionUrl = "jdbc:jtds:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true";
        Connection con = DriverManager.getConnection(connectionUrl);



回答3:


I had the same issue. Its because of the wrong format of the ConnectionUrl. You are missing username and password in the ConnectionUrl.

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

I hope it works well!!!




回答4:


This post my help: jdbc.SQLServerException: Login failed for user for any user

You need the integratedSecurity=true flag and make sure the server properties are indeed set to 'Windows Authentication Mode' under Security.



来源:https://stackoverflow.com/questions/14945075/receiving-sqlexception-login-failed-for-user-connecting-to-sql-server-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!