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?

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
T14h1r
  • 79
  • 10
  • Please see [this question](https://stackoverflow.com/questions/16497998/jdbc-connection-to-mssql-server-in-windows-authentication-mode) – Pizza eu Dec 17 '18 at 07:59
  • With integrated security you cannot specify a username and password (or at least: it will be ignored); that is the whole point. – Mark Rotteveel Dec 17 '18 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? – T14h1r Dec 17 '18 at 11:25
  • Possible duplicate of [SQL Server log in failing from Java DriverManager.getConnection(), working from Python with pymssql.connect()](https://stackoverflow.com/questions/45971084/sql-server-log-in-failing-from-java-drivermanager-getconnection-working-from) – Gord Thompson Dec 17 '18 at 11:52
  • Thanks Gord, It worked. – T14h1r Jan 07 '19 at 13:55

2 Answers2

1

Are you using JTDS driver?

If so,

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

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
IMParasharG
  • 1,869
  • 1
  • 15
  • 26
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;
Vasanth Raghavan
  • 162
  • 2
  • 12
  • 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. – T14h1r Dec 17 '18 at 08:16