2

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

Ramesh Subramanian
  • 944
  • 1
  • 12
  • 28
gtfisher27
  • 31
  • 1
  • 6

1 Answers1

0

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);
........
Ramesh Subramanian
  • 944
  • 1
  • 12
  • 28
  • Thanks< I will give this a try tomorrow, unfortunately, I am dependent on remote access to the site and I can't get access until tomorrow – gtfisher27 Jan 21 '19 at 14:42
  • I tried that I first got an error "Native SSPI library not loaded.". I downloaded the "ntlmauth.dll" for JTDS 1.3.0 and placed it in the JAVA\Bin folder and ran the jar again. And now I get I/O Error: DB server closed connection. This implies to me a config issue on the server and not an authentication error. Is that correct? – gtfisher27 Jan 22 '19 at 20:28
  • I have overcome my immediate problem by getting mixed mode Authentication enabled on the DB and using a SQL user to access. I would still like to know if it possible to use Windows Auth and provide user credentials via the connection string or at least some other means than the logged in user – gtfisher27 Jan 23 '19 at 08:31
  • @gtfisher27 - updated the answer , please check this works on your environment. – Ramesh Subramanian Jan 23 '19 at 09:59
  • I would not recommend the use of jTDS anymore. There were no updates to the project since 2013. That means it does not officially support Sql server 2014 and up. – Peter Schuetze Jun 14 '19 at 16:45