我需要使用JDBC连接到使用Windows身份验证的SQL Server,其中我从连接字符串中提供用户名和密码。Windows身份验证是否可以做到这一点?
我已经用JTDS和msql-jdbc尝试过了,但是不能正常工作。
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;
}
我尝试了用户名和域的各种组合,但通常得到的结果如下:
019-01-18 14:15:31错误com.pts.demo.service.JdbcService -用户'/‘登录失败。ClientConnectionId:962eeab5-226c-4f85-9911-644a570529ab
任何帮助都非常感谢
发布于 2019-01-21 19:02:53
必须在连接字符串useNTLMv2=true
和domain=yourdomain
中包括属性
可能是重复的:Sql Server - connect with windows authentication
还有另一种方法-- https://thusithamabotuwana.wordpress.com/2012/07/19/connecting-to-sql-server-from-java/
您还可以尝试使用以下代码来获取SQL JDBC连接(用户名和密码在props文件中提供,而不是在连接字符串中指定)
............
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);
........
https://stackoverflow.com/questions/54285752
复制相似问题