1. 程式人生 > 實用技巧 >sqlserver用windows方式驗證登入踩過的坑

sqlserver用windows方式驗證登入踩過的坑

坑位一: 之前沒用過windows驗證方式登入sqlserver,連線串怎麼寫

坑位二: 連結上了,但是啟動報錯

八月 19, 2020 9:33:43 上午 com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
警告: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
com.microsoft.sqlserver.jdbc.SQLServerException: 沒有為整合身份驗證配置驅動程式。 ClientConnectionId:8167e4ba
-3567-4871-bdaa-fee41511e3fa at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667) at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<init>(AuthenticationJNI.java:60) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:
2229) at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:
1715) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827) at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:621) at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:57) at OThinker.datasource.TestSqlbyDS.GetResutls1(TestSqlbyDS.java:29) at OThinker.datasource.TestSqlbyDS.main(TestSqlbyDS.java:9) Caused by: java.lang.UnsatisfiedLinkError: no sqljdbc_auth in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<clinit>(AuthenticationJNI.java:35) ... 12 more

坑位三: sqljdbc_auth.dll依賴包加了,但還是報錯

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.microsoft.sqlserver.jdbc.AuthenticationJNI.GetDNSName(Ljava/lang/String;[Ljava/lang/String;Ljava/util/logging/Logger;)I
    at com.microsoft.sqlserver.jdbc.AuthenticationJNI.GetDNSName(Native Method)
    at com.microsoft.sqlserver.jdbc.AuthenticationJNI.GetDNSName(AuthenticationJNI.java:109)
    at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<init>(AuthenticationJNI.java:63)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2229)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:621)
    at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:57)
    at OThinker.datasource.TestSqlbyDS.GetResutls1(TestSqlbyDS.java:29)
    at OThinker.datasource.TestSqlbyDS.main(TestSqlbyDS.java:9)

坑位四:啟動報錯

com.microsoft.sqlserver.jdbc.SQLServerException: 登入失敗。該登入名來自不受信任的域,不能與整合身份驗證一起使用。 ClientConnectionId:120bcc53-18a5-4dc2-bb7f-5addbaf041b0
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:621)
    at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:57)
    at OThinker.datasource.TestSqlbyDS.GetResutls1(TestSqlbyDS.java:29)
    at OThinker.datasource.TestSqlbyDS.main(TestSqlbyDS.java:9)

填坑一:

  網上相關資料也不多,但是多查查還是有的,去掉賬號和密碼,加上integratedSecurity=true

  #jdbc.username=sa
  #jdbc.password=1qaz2wsx#EDC
  jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=test;integratedSecurity=true
  jdbc.dbType=sqlserver

填坑二:

  下載sqljdbc_auth.dll包:https://www.microsoft.com/en-us/download/details.aspx?id=11774

填坑三:

  這個問題應該是sqljdbc_auth.dll版本不對,我用的sqlserver2017版本,但是sqljdbc_auth.dll是在網上下的4版本,所以報坑位三的錯誤,解決方案,用填坑二的連線在官網下載最新版

填坑四:

  將servername設定為localhost我原先用的127.0.0.1,具體為什麼還不瞭解,歡迎大佬留下評論

  jdbc:sqlserver://localhost:1433;DatabaseName=test;integratedSecurity=true

附sqljdbc_auth.dll 4版本的包:

  連結:https://pan.baidu.com/s/1pnUDPcNLF6A297mYTM0uuw

  提取碼:0x46

示例程式碼:兩種方法都可以,用一種測試就行將資料庫和獲取查詢集合結果欄位換成自己的就可以

package koukay.datasource;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

import java.sql.*;

public class SqlServerWinCon {
    public static void main(String[] args) {
        GetResutls1();
        GetResutls2();
    }

    public static void GetResutls1() {
        // Declare the JDBC objects.
        Connection con = null;
        CallableStatement cstmt = null;
        ResultSet rs = null;

        try {
            // Establish the connection.
            SQLServerDataSource ds = new SQLServerDataSource();
//            ds.setUser("sa");
//            ds.setPassword("123456");
            ds.setIntegratedSecurity(true);
//            ds.setServerName("127.0.0.1");//資料庫例項名
            ds.setServerName("localhost");//資料庫例項名
ds.setPortNumber(1433); ds.setDatabaseName("test");//Database Name con = ds.getConnection(); // Execute a SQL that returns some data. cstmt = con.prepareCall(" select top 10 * from test ");//Sql rs = cstmt.executeQuery(); // Iterate through the data in the result set and display it. while (rs.next()) { System.out.println("HeartBeatTimeout: " + rs.getInt("HeartBeatTimeout")); System.out.println("NetworkFailureSleepTime: " + rs.getInt("NetworkFailureSleepTime")); System.out.println("MemoryUsageRate: " + rs.getFloat("MemoryUsageRate")); } } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) { } } if (cstmt != null) { try { cstmt.close(); } catch (Exception e) { } } if (con != null) { try { con.close(); } catch (Exception e) { } } } } public static void GetResutls2() { String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // String URL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test"; String URL = "jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName=test"; Connection con = null; ResultSet rs = null; Statement st = null; String sql = "select * from test"; try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection(URL); st = con.createStatement(); rs = st.executeQuery(sql); if (rs != null) { ResultSetMetaData rsmd = rs.getMetaData(); int countCols = rsmd.getColumnCount(); for (int i = 1; i <= countCols; i++) { if (i > 1) System.out.print("; "); System.out.print(rsmd.getColumnLabel(i)); } System.out.println(""); while (rs.next()) { System.out.println(rs.getInt("HeartBeatTimeout")); System.out.println(rs.getInt("NetworkFailureSleepTime")); System.out.println(rs.getFloat("MemoryUsageRate")); } } } catch (Exception e) { e.printStackTrace(); } try { if (rs != null) rs.close(); if (st != null) st.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } }