2

I need to connect to MS SQL Server using service account.

Currently I am using IntegratedSecurity=true property to enable windows authentication. But in this we are not able to enter password because it takes only the service account using which the application is running.

Could anybody me with the snippet using which I can enter service account and password in the java code to connect to the MS SQL Server?

5
  • Please see this question Commented Dec 17, 2018 at 7:59
  • With integrated security you cannot specify a username and password (or at least: it will be ignored); that is the whole point. Commented Dec 17, 2018 at 11:02
  • Thanks Mark for your comment. Okay I was also trying to that but no luck. Apart from this any other way can you suggest? Commented Dec 17, 2018 at 11:25
  • Possible duplicate of SQL Server log in failing from Java DriverManager.getConnection(), working from Python with pymssql.connect() Commented Dec 17, 2018 at 11:52
  • Thanks Gord, It worked. Commented Jan 7, 2019 at 13:55

2 Answers 2

1

Are you using JTDS driver?

If so,

You have to pass user=abc;domain=CPD.Intr.Service;useNTLMv2=true

Sign up to request clarification or add additional context in comments.

Comments

0

Are you looking for something like this :

Connection conn = DriverManager.getConnection("jdbc:sqlserver://HOSP_SQL1.company.com;user=name;password=abcdefg;database=Test");
        System.out.println("test");
        Statement sta = conn.createStatement();
        String Sql = "select * from testing_table";
        ResultSet rs = sta.executeQuery(Sql);
        while (rs.next()) {
            System.out.println(rs.getString("txt_title"));
        }

You will need the following import statements :

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

1 Comment

Thanks Vasanth for your time and reply. But I already have written the code to connect to database but my requirement is to use service account as a user and from code I can pass password also for the service account.

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.