T14h1r
T14h1r

Reputation: 79

Java code to connect to MS SQL Server using service account and password

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?

Upvotes: 2

Views: 2133

Answers (2)

IMParasharG
IMParasharG

Reputation: 1895

Are you using JTDS driver?

If so,

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

Upvotes: 1

Vasanth Raghavan
Vasanth Raghavan

Reputation: 162

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;

Upvotes: 0

Related Questions