12

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

1
  • How does this relate to android? Commented Oct 7, 2012 at 13:42

6 Answers 6

27

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;");
Sign up to request clarification or add additional context in comments.

6 Comments

I get: Oct 07, 2012 9:45:49 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit> WARNING: Failed to load the sqljdbc_auth.dll Couldn't get database connection.
http://www.microsoft.com/en-us/download/details.aspx?id=21599#filelis Here you can find sqljdbc_auth.dll file. Yous can copy it into c:/windows/system32 (for 32-bit system)
Did it. still WARNING: Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path
Nevermind, I got it to work with the db user I created. Thanks
Mike, open the SQL Server error log to see which user is attempting to log-in to the db instance: C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Log
|
8

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)

2 Comments

It worked. I created a user, but when i'm logging in with the user and try to access the DB, I get: The database SocialFamilyTree is not accessible (ObjectExplorer) how can I give permission to this user.
I forgot to enable mixed mode authentication, thanks for the reminder, worked for me.
4

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.

Comments

1

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)

Comments

0

I also faced the same issue, In my case the following things are configured wrongly

  1. Two SQL (versions) servers are running in my system --> Sol: Please check ourselves which server we are pointing.
  2. Ports are configured as dynamic --> Sol: we should set port 1433 and dynamic port should be 0, if we are connected to specific port.
  3. While creating the new login (user) I have selected the option " change password after first login "--> Sol: we should not select this option while creating the new login, if we are trying connecting from some other service like Openfire.

Comments

0

For me, for some reason, when using a SQL Server instance in the connection string, I had to remove the port number.

From this:

spring.datasource.url=jdbc:sqlserver://servername\\SQL2022:1433;

To this:

spring.datasource.url=jdbc:sqlserver://servername\\SQL2022;

Comments

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.