Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jdbc.SQLServerException: Login failed for user for any user

I trying to test the connection with my local sql DB. I have this code:

try{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=SocialFamilyTree;user=SOSCOMP");

}catch(Exception e){
    System.out.println("Couldn't get database connection.");
    e.printStackTrace();
}

I tried many users. my windows user is SOSCOMP and doesn't have a password. I also know that SQL 2008 create users as "sys" "dbo", I tried these too. I'm always getting:

Couldn't get database connection.
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'SOSCOMP'.

    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2532)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:243)
    at FT_Receiver.FT_Receiver.main(FT_Receiver.java:12)

Any ideas?

Thanks

like image 231
Mike Avatar asked Oct 07 '12 13:10

Mike


4 Answers

If you try to connect with database which is using windows authentication, you can use 'integratedSecurity' option in your connection string.

DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=SocialFamilyTree;integratedSecurity=true;");
like image 145
piotrp Avatar answered Nov 10 '22 09:11

piotrp


Having been through this very recently the steps I took to solve pretty much the same problem were

  1. use SQL Server Management Studio to log in with the desired account and confirm access to read (and write if necessary)
  2. Use SQL Server Configuration Manager to confirm that the server instance is listening on the IP address being targetted
  3. Disable the firewall to check that isn't getting in the way (and add an exception if necessary for future use)

The absolute kicker for me was understanding what IP addresses and ports the instance was set to listen on so that when I constructed the connection string the connection wasn't being rejected.

Also, if you want to connect using Windows logins you need to ensure the SQL instance is configured for mixed mode authentication (i.e. to allow Windows and SQL logins)

like image 37
Paul D'Ambra Avatar answered Nov 10 '22 11:11

Paul D'Ambra


Since you get this error,the Sql server correctly listens to the port.

  1. Open Sql Server Management Studio connect to your Server.
  2. right click on the server's icon and choose properties.
  3. Go to the security tab and tick Sql Server and Windows Authentication mode.

If you want to define a user,go from the tree, to Security->Logins,right click on logins folder and click "New Login".

Now your server should work with this Url String. Use the log file of the Server that may help you understand its working.

like image 45
Vaggos Phl Avatar answered Nov 10 '22 11:11

Vaggos Phl


Re: Did it. still WARNING: Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path – Mike Oct 7 at 14:03

you have to add the path to sqljdbc_auth.dll by adding this under VM arguments in Eclipse or commandline if you're running from the shell: -Djava.library.path="\MS SQL Server JDBC Driver 3.0\sqljdbc_3.0\enu\auth\x86"

that's if you're running 32 bit Windows. else the final subdir changes accordingly.

I think this might be a better answer though, to setting up SQL Server user based authentication: Connecting SQL Server 2008 to Java: Login failed for user error

(I try to summarize it here: http://silveira.wikidot.com/sql-server)

like image 1
Nikhil Silveira Avatar answered Nov 10 '22 09:11

Nikhil Silveira