Can a Java application running on a Window's Server connect to SQL Server via Windows Authentication

旧巷老猫 提交于 2019-12-05 12:24:25

You can connect to SQL Server from Java programs using windows authentication as follows:

  1. Create a windows account for the application that would be used to run your programs. This account's credentials will be used to connect to the server.
  2. Get Microsoft JDBC Driver for SQL Server from here.
  3. Configure the JDBC URL as follows:

    jdbc:sqlserver://<hostname>;databaseName=<DBName>;integratedSecurity=true
    
  4. Configure the launcher that run the Java programs from command line to include the following JVM parameter:

    -Djava.library.path="<jdbc driver dll location>"
    

    where the location is the directory where the JDBC driver downloaded earlier is installed or extracted. It was C:\Program Files\sqljdbc_4.0.2206.100_enu\sqljdbc_4.0\enu\auth\x64 in my case. As Luke Woodward mentioned in the comments, the dll should be picked based on the JVM used for running these programs.

With the above configuration, the connection established to SQL Server would use the Windows Authentication Credentials of the domain user running the java program/process.

The first step is to setup ODBC, you can go to Control panel -> Administrative tools -> ODBC. Add a new DSN to connect MS SQL Server using windows authentication account following wizard setup. The second step is the same as using SQL Server authentication account. But the only change is that the connection string is changed to: jdbc:odbc:dsn-name. There is no need to use username/password anymore, because it is already connected to the server.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!