1. 程式人生 > >java使用windows驗證方式連接sqlserver2008數據庫

java使用windows驗證方式連接sqlserver2008數據庫

log 代碼 hid ogg rman nal sta args ron

連接數據庫的代碼段:

技術分享圖片
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 >

java使用windows驗證方式連接sqlserver2008數據庫