2

I'm trying to connect to my database using sqljdbc4, I'm pretty new to this so i followed a couple of tutorial but it still doesn't seem to work, When i try to run the program i get this Exception:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''.        ClientConnectionId:f181fd37-7e28-4392-ac86-02914c2090e1
at  com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216) 

And this is my code:

    try {

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DBank;","","");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
         } catch (SQLException e) {
            e.printStackTrace(); 
         }
    return null;

2 Answers 2

5

You are not passing any authentication credentials. If you want to use integrated security (windows authentication), then you need to explicitly specify that in your connection URL and you need to load the native library required for this.

See Connecting with Integrated Authentication On Windows on Windows on MSDN.

This essentially comes down to including the folder containing the (32 bit or 64 bit) sqljdbc_auth.dll in the java.library.path system property (see link for details) and adding integratedSecurity=true to your connection string:

DriverManager.getConnection(
   "jdbc:sqlserver://localhost:1433;databaseName=DBank;integratedSecurity=true");
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much!!! i was searching hours for a solution and couldn't understand why it's not working, Working now.
I was messing around with this for quiet some time. I was using the driver through Proxool and specifying "integratedSecurity" as a Property which seemed like it should have worked according to the MS docs. Sure enough, moving to the URL solved the issue. Thanks!
1

You are passing empty username and password while connecting to the database in getConnection method:

DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DBank;","","");

Try supplying the db username and password, for example:

DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=DBank;","myusername","mypassword");

1 Comment

I dont have password and username configured in my database, I've removed the ,"","" , But i still get the same exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.