java使用windows验证方式连接sqlserver2008数据库

时间:2022-07-14 07:11:40

 

连接数据库的代码段:

java使用windows验证方式连接sqlserver2008数据库java使用windows验证方式连接sqlserver2008数据库
package com.db;

import java.sql.*;

public class DButil {
public static void main(String[] args) {
Connection con
= null;
Statement stmt
= null;
ResultSet rs
= null;
String driver
= "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url
= "jdbc:sqlserver://10.103.2.18:1433;databaseName=HCM;integratedSecurity=true;";//windows集成模式连接
try {
Class.forName(driver);
con
= DriverManager.getConnection(url);
System.out.println(
"Connecting Successful!!!!!");
stmt
= con.createStatement();
rs
= stmt.executeQuery("select top 10 * from EmpData");
while(rs.next()){
System.out.println(rs.getString(
"EmployeeID")+"\t"+rs.getString("Name"));
}
}
catch (SQLException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally{
try {
rs.close();
stmt.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}

}

}

}
View Code

 

出现的异常有:

 Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path

 解決方案:

将sqljdbc_auth.dll加入C:\Windows\System32中

 

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.microsoft.sqlserver.jdbc.AuthenticationJNI.GetDNSName(Ljava/lang/String[Ljava/lang/String;Ljava/util/logging/Logger;)I

解決方案:

1.将sqljdbc4.jar加入工程
2.将sqljdbc_auth.dll文件copy到以下目录

  • c:/windows/system32
  • eclipse所用jdk的bin目录下
  • eclipse所用jre的bin目录下

 <sqljdbc_auth.dll来自JDBC Driver X.0 for SQL Server,下载网址 http://www.microsoft.com/en-us/download/details.aspx?id=11774 >