WindowsManager getCredential

问题描述 投票:0回答:0

 
我设置了 Snowflake JDBC 连接,但是当我尝试从表中获取一些数据时,出现以下错误:

Apr 17, 2023 3:22:47 AM net.snowflake.client.core.SecureStorageWindowsManager getCredential
INFO: Failed to read target or could not find it in Windows Credential Manager. Error code = 1168

public static void destroy(){ // 我们必须在关闭资源之前先检查我们是否拥有有效的对象 // 因为我们不能对不存在的对象采取行动 尝试 { 如果( rs!=null) rs.close(); 如果(stm!=null)stm.close(); 如果( con!=null) con.close(); } 赶上(异常 e){ System.out.println("关闭资源时出错" + e.getMessage() ); }

}

/**
 * This method will reset the cursor to before first location
 */


private static void resetCursor(){

    try {
        rs.beforeFirst();
    } catch (Exception e) {
        e.printStackTrace();
    }





}



// find out the row count
/**
 * find out the row count
 * @return row count of this ResultSet
 */
public static int getRowCount(){

    int rowCount = 0 ;
    try {
        rs.last() ;
        rowCount = rs.getRow() ;
    } catch (Exception e) {
        System.out.println("ERROR OCCURRED WHILE GETTING ROW COUNT " + e.getMessage() );
    }finally {
        resetCursor();
    }

    return rowCount ;

}


/**
 * find out the column count
 * @return column count of this ResultSet
 */
public static int getColumnCount(){

    int columnCount = 0 ;

    try {
        columnCount = rsmd.getColumnCount();

    } catch (Exception e) {
        System.out.println("ERROR OCCURRED WHILE GETTING COLUMN COUNT " + e.getMessage() );
    }

    return columnCount ;

}


// get entire row of data according to row number
/**
 * Getting entire row of data in a List of String
 * @param rowNum row number to get as a list
 * @return row data as List of String
 */
public static List<String> getRowDataAsList( int rowNum ){

    List<String> rowDataAsLst = new ArrayList<>();
    int colCount =  getColumnCount() ;

    try {
        rs.absolute( rowNum );

        for (int colIndex = 1; colIndex <= colCount ; colIndex++) {

            String cellValue =  rs.getString( colIndex ) ;
            rowDataAsLst.add(   cellValue  ) ;

        }


    } catch (Exception e) {
        System.out.println("ERROR OCCURRED WHILE getRowDataAsList " + e.getMessage() );
    }finally {
        resetCursor();
    }


    return rowDataAsLst ;
}

/**
 * getting the cell value according to row num and column index
 * @param rowNum  row number to get the data from
 * @param columnIndex column number to get the data from
 * @return the value in String at that location
 */
public static String getCellValue(int rowNum , int columnIndex) {

    String cellValue = "" ;

    try {
        rs.absolute(rowNum) ;
        cellValue = rs.getString(columnIndex ) ;

    } catch (Exception e) {
        System.out.println("ERROR OCCURRED WHILE getCellValue " + e.getMessage() );
    }finally {
        resetCursor();
    }
    return cellValue ;

}


/**
 * getting the cell value according to row num and column name
 * @param rowNum  row number to get the data from
 * @param columnName column Name to get the data from
 * @return the value in String at that location
 */
public static String getCellValue(int rowNum ,String columnName){

    String cellValue = "" ;

    try {
        rs.absolute(rowNum) ;
        cellValue = rs.getString( columnName ) ;

    } catch (Exception e) {
        System.out.println("ERROR OCCURRED WHILE getCellValue " + e.getMessage() );
    }finally {
        resetCursor();
    }
    return cellValue ;

}


/**
 * Get First Cell Value at First row First Column
 */
public static String getFirstRowFirstColumn(){

    return getCellValue(1,1) ;

}


/**
 * getting entire column data as list according to column number
 * @param columnNum column number to get all data
 * @return List object that contains all rows of that column
 */
public static List<String> getColumnDataAsList(int columnNum){

    List<String> columnDataLst = new ArrayList<>();

    try {
        rs.beforeFirst(); // make sure the cursor is at before first location
        while( rs.next() ){

            String cellValue = rs.getString(columnNum) ;
            columnDataLst.add(cellValue) ;
        }

    } catch (Exception e) {
        System.out.println("ERROR OCCURRED WHILE getColumnDataAsList " + e.getMessage() );
    }finally {
       resetCursor();
    }


    return columnDataLst ;

}


/**
 * getting entire column data as list according to column Name
 * @param columnName column name to get all data
 * @return List object that contains all rows of that column
 */
public static List<String> getColumnDataAsList(String columnName){

    List<String> columnDataLst = new ArrayList<>();

    try {
        rs.beforeFirst(); // make sure the cursor is at before first location
        while( rs.next() ){

            String cellValue = rs.getString(columnName) ;
            columnDataLst.add(cellValue) ;
        }

    } catch (Exception e) {
        System.out.println("ERROR OCCURRED WHILE getColumnDataAsList " + e.getMessage() );
    }finally {
         resetCursor();
    }


    return columnDataLst ;

}



/**
 * display all data from the ResultSet Object
 * @return
 */
public static List<String> displayAllData(){

    int columnCount = getColumnCount() ;
    resetCursor();
    try{

        while(rs.next()){

            for (int colIndex = 1; colIndex <= columnCount; colIndex++) {

                //System.out.print( rs.getString(colIndex) + "\t" );
                System.out.printf("%-25s", rs.getString(colIndex));
            }
            System.out.println();

        }

    }catch(Exception e){
        System.out.println("ERROR OCCURRED WHILE displayAllData " + e.getMessage() );
    }finally {
        resetCursor();
    }

    return null;
}


/**
 * Save entire row data as Map<String,String>
 * @param rowNum row number
 * @return Map object that contains key value pair
 *      key     : column name
 *      value   : cell value
 */
public static Map<String,String> getRowMap(int rowNum){

    Map<String,String> rowMap = new LinkedHashMap<>();
    int columnCount = getColumnCount() ;

    try{

        rs.absolute(rowNum) ;

        for (int colIndex = 1; colIndex <= columnCount ; colIndex++) {
            String columnName = rsmd.getColumnName(colIndex) ;
            String cellValue  = rs.getString(colIndex) ;
            rowMap.put(columnName, cellValue) ;
        }

    }catch(Exception e){
        System.out.println("ERROR OCCURRED WHILE getRowMap " + e.getMessage() );
    }finally {
        resetCursor();
    }


    return rowMap ;
}


/**
 * We know how to store one row as map object
 * Now Store All rows as List of Map object
 * @return List of Map object that contain each row data as Map<String,String>
 */
public static List<Map<String,String>> getAllRowAsListOfMap(){

    List<Map<String,String>> allRowLstOfMap = new ArrayList<>();
    int rowCount = getRowCount() ;
    // move from first row till last row
    // get each row as map object and add it to the list

    for (int rowIndex = 1; rowIndex <= rowCount ; rowIndex++) {

        Map<String,String> rowMap = getRowMap(rowIndex);
        allRowLstOfMap.add( rowMap ) ;

    }
    resetCursor();

    return allRowLstOfMap ;

}






public static void createConnectionforSnowflake(String snowflakehost , int snowflakeport, String snowflakeusername,String snowflakepassword,String snowflakerole, String snowflakedatabase,String snowflakeschema,String snowflakewarehouse, String snowflakeurl){ 
  try { 
    con = DriverManager.getConnection(snowflakeurl, snowflakeusername, snowflakepassword) ;     
    System.out.println("Successfully connected to Snowflake!");
  } catch (SQLException e) { 
    e.printStackTrace(); 
  } 
}

public static ResultSet snowflakerunQuery(String sql){ 

  try Class.forName("com.snowflake.client.jdbc.SnowflakeDriver"); 
    stm = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    stm.executeQuery("");
    stm.executeQuery("");
    stm.executeQuery("");
    stm.executeQuery(""); 
    rs = stm.executeQuery(sql); // setting the value of ResultSet   
    objectrsmd = rs.getMetaData() ; // setting the value of ResultSetMetaData for reuse 
  } catch(Exception e){ 
    System.out.println("ERROR OCCURRED WHILE RUNNING QUERY "+ e.getMessage() ); 
  } 
  return rs ; 
}

可能是什么问题?

java jdbc automation snowflake-cloud-data-platform backend
© www.soinside.com 2019 - 2024. All rights reserved.