2011-06-18 4 views
-2
  1. 以下のコードでは、ループ内で印刷された子ハッシュマップは正しい値を示していますが、親ハッシュマップがループ外に出力された場合、子ハッシュマップは最後のエントリはすべての値を上書きします。ハッシュマップを別のコレクションに入れたときの問題

    public void compareOracleMySQLData() throws SQLException { 
        String inputTableName = ConfigurationManager.getProperty("table_name"); 
    
        int i = 0; 
        int j = 0; 
        int colCount = 0; 
        int oracleRowCount = 0; 
        int mysqlRowCount = 0; 
        String primaryKeyIni = null; 
        String appendStuff = null; 
        Connection conO = DBManager.openDbConnection("mysql"); 
        Connection conM = DBManager.openDbConnection("mysql"); 
        Statement stmtO = null; 
        Statement stmtM = null; 
        ResultSet resultSetO = null; 
        ResultSet resultSetM = null; 
        ArrayList<String> primaryKeyList = new ArrayList<String>(); 
        Iterator<String> primaryKeyListItr = null; 
        HashMap<Object, Object> oraRowDetailsMap = new HashMap<Object, Object>(); 
        HashMap<String, Object> mysqlRowDetailsMap = new HashMap<String, Object>(); 
        Map<String, Object> oraRowPrimaryMap = new HashMap<String, Object>(); 
        HashMap<String, HashMap<String, Object>> mysqlRowPrimaryMap = new HashMap<String, HashMap<String, Object>>(); 
    // Map<String, Object> oraRowPrimaryMap=new HashMap<String, Object>(); 
    // Map<String, Object> mysqlRowPrimaryMap=new HashMap<String, Object>(); 
        try { 
         if (conO != null && conM != null) { 
          if (validateTableStatus(inputTableName, conO, conM)) { 
           // Check table existence in Oracle and Mysql Database 
    
           // Create resultset for oracle 
           stmtO = conO.createStatement(
             ResultSet.TYPE_SCROLL_INSENSITIVE, 
             ResultSet.CONCUR_READ_ONLY); 
           resultSetO = stmtO.executeQuery(DB_QUERY); 
           ResultSetMetaData rsMetaDataO = resultSetO.getMetaData(); 
    
           // 
    
           // MySql details for the same table created 
           stmtM = conM.createStatement(
             ResultSet.TYPE_SCROLL_INSENSITIVE, 
             ResultSet.CONCUR_READ_ONLY); 
           resultSetM = stmtM.executeQuery(DB_QUERY); 
    
           // Get Column Count for two tables 
           ResultSetMetaData rsMetaDataM = resultSetM.getMetaData(); 
           logger.debug("Column Count in Oracle table ::" 
             + rsMetaDataO.getColumnCount()); 
           logger.debug("Column Count in Mysql table ::" 
             + rsMetaDataM.getColumnCount()); 
           // Match Column count of two tables 
           if (rsMetaDataM.getColumnCount() == rsMetaDataO 
             .getColumnCount()) { 
            logger.debug("The Column count in both table are same"); 
            oracleRowCount = getRowCount(inputTableName, resultSetO); 
            mysqlRowCount = getRowCount(inputTableName, resultSetM); 
            logger.debug("No of Rows of Oracle Table :: " 
              + oracleRowCount); 
            logger.debug("No of Rows of Mysql Table :: " 
              + mysqlRowCount); 
            if (oracleRowCount != mysqlRowCount) { 
             logger 
               .debug("The number of rows of Oracle and Mysql tables respectively differs"); 
    
            } 
    
    
    
            else { 
             primaryKeyList = getPrimaryKey(inputTableName, conO); 
    
             while (resultSetO.next()) { 
    
              i = 0; 
              primaryKeyListItr = primaryKeyList.iterator(); 
              while (primaryKeyListItr.hasNext()) { 
    
               if (i == 0) { 
                primaryKeyIni = (resultSetO 
                  .getObject(primaryKeyListItr 
                    .next().toString())) 
                  .toString().trim(); 
    
    
               } else { 
                appendStuff = (resultSetO 
                  .getObject(primaryKeyListItr 
                    .next().toString())) 
                  .toString().trim(); 
                primaryKeyIni = primaryKeyIni + "$" 
                  + appendStuff; 
    
               } 
               i++; 
    
              } 
    
              // 
    
              colCount = rsMetaDataO.getColumnCount(); 
              for (j = 1; j <= colCount; j++) { 
               System.out.println("Col Name"+rsMetaDataO 
                 .getColumnName(j)); 
               System.out.println("Col Value"+ resultSetO 
                 .getObject(rsMetaDataO 
                   .getColumnName(j))); 
               oraRowDetailsMap.put(rsMetaDataO 
                 .getColumnName(j), resultSetO 
                 .getObject(rsMetaDataO 
                   .getColumnName(j))); 
              } 
               System.out.println("primaryKeyIni"+primaryKeyIni); 
              System.out.println("oraRowDetailsMap inside loop"+oraRowDetailsMap); 
              oraRowPrimaryMap.put(primaryKeyIni, 
                oraRowDetailsMap); 
        } 
        System.out.println("oraRowDetailsMap"+oraRowDetailsMap);System.out.println(oraRowPrimaryMap); 
    
          } 
    
           } else { 
            logger.debug("The number of Columns of Oracle and Mysql table differs"); 
           } 
    
          } 
         } 
        } 
    
        catch (SQLException e) { 
         logger.debug("Error in CompareOracleMySQLData()" + e.getMessage()); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } finally { 
         DBManager.closeConnection(conO); 
         DBManager.closeConnection(conM); 
         DBManager.closeStatement(stmtO); 
         DBManager.closeStatement(stmtM); 
         DBManager.closeResultSet(resultSetO); 
         DBManager.closeResultSet(resultSetM);}} 
    

2.出力は、以下である::

     DEPLOYMENT_LEVEL is: DVL 
DEBUG com.tsh.compass.intranet.dao.DbDao 18 Jun 2011 23:06:39 IST. - Connection to  MYSQL database established 
DEBUG com.tsh.compass.intranet.dao.DbDao 18 Jun 2011 23:06:39 IST. - Connection to  MYSQL database established 
DEBUG com.tsh.compass.intranet.dao.DbDao 18 Jun 2011 23:06:39 IST. - Column Count in Oracle table ::4 
DEBUG com.tsh.compass.intranet.dao.DbDao 18 Jun 2011 23:06:39 IST. - Column Count in Mysql table ::4 
DEBUG com.tsh.compass.intranet.dao.DbDao 18 Jun 2011 23:06:39 IST. - The Column count in both table are same 
DEBUG com.tsh.compass.intranet.dao.DbDao 18 Jun 2011 23:06:39 IST. - No of Rows of Oracle Table :: 3 
DEBUG com.tsh.compass.intranet.dao.DbDao 18 Jun 2011 23:06:39 IST. - No of Rows of Mysql Table :: 3 
Col NameFirst_Name 
Col Valuefn 
Col NameLast_Name 
Col Valueln 
Col NameAddress 
Col Valueadr 
Col NameCity 
Col Valuecity 
primaryKeyInifn$ln 
oraRowDetailsMap inside loop{Address=adr, Last_Name=ln, First_Name=fn, City=city} 
Col NameFirst_Name 
Col Valuefn1 
Col NameLast_Name 
Col Valueln1 
Col NameAddress 
Col Valueadr1 
Col NameCity 
Col Valuecity1 
primaryKeyInifn1$ln1 
oraRowDetailsMap inside loop{Address=adr1, Last_Name=ln1, First_Name=fn1, City=city1} 
Col NameFirst_Name 
Col Valuefn3 
Col NameLast_Name 
Col Valueln3 
Col NameAddress 
Col Valueadr3 
Col NameCity 
Col Valuecity3 
primaryKeyInifn3$ln3 
    oraRowDetailsMap inside loop{Address=adr3, Last_Name=ln3, First_Name=fn3, City=city3} 
    oraRowPrimaryMap{fn3$ln3={Address=adr3, Last_Name=ln3, First_Name=fn3,  City=city3}, fn$ln={Address=adr3, Last_Name=ln3, First_Name=fn3, City=city3}, fn1$ln1=  {Address=adr3, Last_Name=ln3, First_Name=fn3, City=city3}} 
+8

コードの壁 –

+3

最小限の例をコードしてください。 – Matthias

+0

子とは何ですか?親ハッシュマップとは何ですか?私はそれがmysqlRowPrimaryMapかもしれないと思いますが、それはどこにも使われていませんか? – merxbj

答えて

0

あなたは、単に行ごとに同じoraRowDetailsMapを再利用しています。結果に同じマップが何度も繰り返し表示されていることは間違いありません。

結果セットループ内にMap<...> oraRowDetailsMap = new HashMap<...>();を使用すると、ループの前に1回だけでなく、各行に新しいマップを作成できます。

+0

PaŭloEbermannに感謝しました。 – Joy

関連する問題