gtfisher27
gtfisher27

Reputation: 31

Windows authentication with JDBC and SQL Server

I need to connect using JDBC to SQL Server using Windows Authentication where I provide username and password from the connection String. Is this possible with Windows Authentication?

I have tried this with both JTDS and msql-jdbc but can't get it to work

private  Connection getDBConnection() {
  Connection dbConnection = null;
  try {
         System.out.println("load driver");
         Class.forName("net.sourceforge.jtds.jdbc.Driver");
         log.info("loaded");

         String con = "jdbc:jtds:sqlserver://PNT00-PMP-SQL01:1433/iceware;domain=workgroup;userName=user;password=password";

         dbConnection = DriverManager.getConnection(con);

         log.info("got connection");

        return dbConnection;

   } catch (Exception e) {
         log.error(e.getMessage());
   }
   return dbConnection;
 }

I have tried various combinations for the username and domain but usually get something like:

019-01-18 14:15:31 ERROR com.pts.demo.service.JdbcService - Login failed for user '/'. ClientConnectionId:962eeab5-226c-4f85-9911-644a570529ab

Any help much appreciated

Upvotes: 2

Views: 11907

Answers (1)

Ramesh Subramanian
Ramesh Subramanian

Reputation: 1037

You have to include the properties in the connection string useNTLMv2=true and domain=yourdomain

This may be duplicate : Sql Server - connect with windows authentication

There is also another way - https://thusithamabotuwana.wordpress.com/2012/07/19/connecting-to-sql-server-from-java/

Also you can try the below code to get SQL JDBC connection (username & password are provided in the props files instead of specifying them on the connection string)

............
String jdbcUrl = "jdbc:jtds:sqlserver://192.168.10.101:1433/dbName;charset=utf8";
System.setProperty("jsse.enableCBCProtection", "false"); // for SSL enabled MSSQL
Properties prop=new Properties();
prop.put("user",user);
prop.put("password",passWord);
prop.put("domain",domain);
prop.put("useNTLMv2","true");

dbConnection = DriverManager.getConnection(jdbcUrl , prop);
........

Upvotes: 1

Related Questions