2

I am trying to connect to msq sql server 2005 using jdbc.Sql server 2005 is installed in local system and I am trying in the following way.But it fails if i write the ip address of the local system instead of localhost.What would be reason.

import java.io.File;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;
import java.sql.ResultSet;  
import java.sql.Statement;  


public class mssql {  
    public static void main(String[] args) {  
        try {  
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();

            Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.207:1433;databaseName=WindProfiles;integratedSecurity=true;");// If i write localhost instead of 192.168.1.207 then works else shows error
            if(!(connection==null))
            {
                System.out.println("connected");
            }

//            


        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  

error

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. The user is not associated with a trusted SQL Server connection. ClientConnectionId:932514a0-5e3d-4d9c-8080-1e83ec703f9f
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at mssql.main(mssql.java:14)

I know instead of local system i can write 127.0.0.1 but my idea is to connect to remote system also. When i write in command prompt telnet 192.168.1.207 1433 then it connects.Even it connects from remote system also. enter image description here

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • @Kalathoki Yes it does,I have mentioned it in my question(just below the error) – SpringLearner Sep 24 '13 at 04:05
  • Did you follow the checklist I suggest in [your other question yesterday](http://stackoverflow.com/questions/18958654/can-not-connect-to-ms-sql-server-using-windows-authentication-on-remote-system/18959250#18959250)? – cdoubleplusgood Sep 24 '13 at 06:04
  • The error means that the remote server cannot identify your login. What Windows account is your client process running in? Is this a domain account? If you are using a local Windows account, Windows authentication cannot work. – cdoubleplusgood Sep 24 '13 at 06:08
  • @cdoubleplusgoodm I tried the way you said and it shows "Msg 18452, Level 14, State 1, Server DGSXXX, Line 1 Login failed for user ''. The user is not associated with a trusted SQL Server c onnection." – SpringLearner Sep 24 '13 at 06:08
  • @cdoubleplusgood you said what "What Windows account is your client process running in? Is this a domain account? If you are using a local Windows account, Windows authentication cannot work." I did not understand these questions.Earlier I was working with mysql and day before yesterday i started in ms sql server.Please elaborate your questions – SpringLearner Sep 24 '13 at 06:10
  • When using Windows authentication, the server must "know" your Windows login. If your login on the client is a local account (not domain account), the server does not know who you are, and Windows authentication fails. Client and server must be in the same domain, and your login must be a domain account. – cdoubleplusgood Sep 24 '13 at 06:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37917/discussion-between-javabeginner-and-cdoubleplusgood) – SpringLearner Sep 24 '13 at 06:14
  • @cdoubleplusgood can you please come to chat? – SpringLearner Sep 24 '13 at 06:15
  • @cdoubleplusgood can you tell me how to set sql server authentication – SpringLearner Sep 24 '13 at 08:01

2 Answers2

1

You are missing username and password of your database so you are getting this error. use following to solve your problem:

Connection connection = DriverManager.getConnection("jdbc:sqlserver://192.168.1.207:1433;databaseName=WindProfiles","username","password");

Updated

If you wish to connect using windows authentication see following link: ( But I do not have experience using windows authentication mode from java)

  1. Connecting to SQL Server from Java
Yubaraj
  • 3,800
  • 7
  • 39
  • 57
  • Please check whether you have updated host file of your system with the IP address. Also as stated in @Kalathoki post, update ur DriverManager with user name and password. – AKV Sep 24 '13 at 04:23
  • @Kalathoki as i stated in the question,I am trying with windows authentication mode.If it would be sql server authentication then username and password will be there but in windows authentication no such type username and password. – SpringLearner Sep 24 '13 at 04:24
  • @AKV I did not get this line,what you meant by this and how to do "Please check whether you have updated host file of your system with the IP address" – SpringLearner Sep 24 '13 at 04:29
  • Try to use jtds driver instead of jdbc driver.. Also please [check this link](http://stackoverflow.com/questions/167464/can-i-connect-to-sql-server-using-windows-authentication-from-java-ee-webapp) which might be of some help. – AKV Sep 24 '13 at 04:37
  • @AKV Just a simple query i have,can I connect to remote systems using jdbc in windows authentication mode – SpringLearner Sep 24 '13 at 04:42
  • @javaBeginner You can see the link provide by AKV. And also see my updated answer. – Yubaraj Sep 24 '13 at 04:45
  • @Kalathoki Tell me is it possible to connect to remote system in windows authentication? – SpringLearner Sep 24 '13 at 04:46
  • @javaBeginner I do not know because of I do not have experience. Other expert may tell you. But I recommend you to use like my answer using `sql server Authentication`. And why do you want to use only `windwos autnentication`?. – Yubaraj Sep 24 '13 at 04:48
  • 1
    @javaBeginner I haven't worked with Windows authentication mode. May I know what is the reason behind using windows auth mode?. Have you tried working with localhost:1433 in windows auth mode? if so, are you able to connect? – AKV Sep 24 '13 at 04:48
  • @AKV I agree with you. – Yubaraj Sep 24 '13 at 04:49
  • windows authentication mode is more secure. you aren't storing usernames/passwords in a string that can be read out of your program. Many reasons to use windows authentication mode, and yes it can be used to connect to remote or local machines. – BlackICE Sep 24 '13 at 04:59
  • @BlackICE as you can see in the screenshot i do not have password in windows authentication mode,But if i put "" then also its not connecting. – SpringLearner Sep 24 '13 at 05:09
  • @AKV yes i tried with windows authentication mode with localhost:1433 and I have mentioned this in my question too.reason for using windows authentication mode is that in out organization the other team are inserting into database using windows authentication and my work is to retrive it. – SpringLearner Sep 24 '13 at 05:10
  • @Kalathoki in this Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.207:1433;databaseName=WindProfiles;integratedSecurity=true;");// If i write localhost instead of 192.168.1.207 then works else shows error If write localhost then it works but if i write the local ip address then does not work ,why? – SpringLearner Sep 24 '13 at 06:03
  • @javaBeginner And can you telnet `192.168.1.207 1433` ? – Yubaraj Sep 24 '13 at 06:09
  • @Kalathoki see when i do telnet 192.168.1.207 1433 it does not show error but in the command prompt i see only blank.earlier when I was trying it shows"Connecting To 192.168.1.207...Could not open connection to the host, on port 143 3: Connect failed" – SpringLearner Sep 24 '13 at 06:12
  • @javaBeginner If you are using windows cmd you can see on your cmd window title as like : `Telnet 127.0.0.1`. Hey it is easy man. If not working you can see only `connection to 127.0.0.1... could not open to the host , on port 1433: connection failled` – Yubaraj Sep 24 '13 at 09:30
  • @javaBeginner Then it is success. And you may also see `Telnet 127.0.0.1` on header of cmd. – Yubaraj Sep 24 '13 at 09:32
1

There are a couple of ways I know of to do integrated security to MSSQL, the connection string option you are using is unfamiliar to me (may be valid for java, i don't know). Here are the options I know of to use trusted connection:

Trusted_Connection=True

Integrated Security=SSPI

Try replacing the integratedSecurity=true portion of your connection string with one of those and see if it works.


Edit

Can you try adding another catch with this code:

catch (SQLException se) {
      do {
         System.out.println("SQL STATE: " + se.getSQLState());
         System.out.println("ERROR CODE: " + se.getErrorCode());
         System.out.println("MESSAGE: " + se.getMessage());
         System.out.println();
         se = se.getNextException();
      } while (se != null);
   }

You can try following the suggestions here also, which is what I'm looking at:

http://msdn.microsoft.com/en-us/library/ms378522.aspx

BlackICE
  • 8,816
  • 3
  • 53
  • 91
  • I tried both the ways but it does not work,showing this error "Login failed for user 'DGSXXX\spanwave'. ClientConnectionId:528fc0b9-82c5-4c94-99f7-ee4fc29bae3b" – SpringLearner Sep 24 '13 at 05:16
  • also, what version of jdbc are you using? might want to try using jdbc4 and see if that helps. – BlackICE Sep 24 '13 at 05:19
  • I use sqljdbc4.jar. See I have just created a local database and tried to connect using jdbc in windows authentication.It worked.Now I wanted to connect to remote server but fails. – SpringLearner Sep 24 '13 at 05:21
  • this is the output i am getting after adding catch as you suggested. "SQL STATE: S0001 ERROR CODE: 18456 MESSAGE: Login failed for user 'DGSXXX\spanwave'. ClientConnectionId:474f9c07-42c8-4bef-9c0e-a54bfd26df71" – SpringLearner Sep 24 '13 at 05:45
  • In this Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.207:1433;databaseName=WindProfiles;integratedSecurity=true;");// If i write localhost instead of 192.168.1.207 then works else shows error if I write localhost then it works but if i give the local ip address then not working ,why? – SpringLearner Sep 24 '13 at 06:02
  • do you have SSMS or query analyzer, can you connect using the IP address instead of localhost using another program? – BlackICE Sep 24 '13 at 10:53
  • Using jtds i can connect using ip address.I can connect using 192.168.1.207 or localhost or 127.0.0.1.But on the remote side its showing error – SpringLearner Sep 24 '13 at 10:57