1.在微软官方网站下载相应的 Microsoft JDBC Driver for SQL Server
Download - JDBC Driver for SQL Server | Microsoft Docs
2.解压下载的驱动包:比如我下载的是sqljdbc_9.2.1.0_enu,然后进行解压
3.将auth下的64位的dll copy到C:\Windows\System32下
4.在IDEA中导入步骤二中的mssql-jebc-9.2.1.jire8
5.通过以下代码测试;可以实现windows身份认证
public static void main(String[] args) {
String connectionUrl = "jdbc:sqlserver://127.0.0.1;databaseName=AdventureWorks2012;integratedSecurity=true";
try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();) {
String SQL = "SELECT TOP 10 * FROM Person.Person";
ResultSet rs = stmt.executeQuery(SQL);
while (rs.next()) {
System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName"));
}
}
catch (SQLException e) {
e.printStackTrace();
}
}