2015-12-20 18 views
0

我想取一列中的所有數值的平均值&用計算平均值更新該列中的空單元格但是在更新空單元格時遇到問題....請給我建議......關注是我的代碼...如何使用該列中數值的平均值更新特定列的空單元格? (使用Java的Mysql)

/*---------------- Function to apply Mean Imputation Algorithm on saved data in Database -----------------*/ 
public void CalcMean(String tableName) { 

     Statement stmt = null; 
     String sql = null; 
     ResultSet rs = null; 

     try { 

      setConnection(); 
      //connection.setAutoCommit(false); 
      stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
        ResultSet.CONCUR_UPDATABLE); 

      sql = "SELECT * FROM " + tableName; 
      System.out.println("sql :" + sql); 
      rs = stmt.executeQuery(sql); 
      CachedRowSet rowset = new CachedRowSetImpl(); 
      rowset.populate(rs); 

      while(rowset.next()){ 

       ResultSetMetaData rsmd = rowset.getMetaData(); 
       int numOfCols = rsmd.getColumnCount();//get total number of columns from database 

        for(int i = 1; i <= numOfCols; i++){ 
         if (rowset.getString(i)== null || rowset.getString(i).equals("*") || rowset.getString(i).equals("?")) 
         { 
          System.out.println("was NULL"+i);// Database contains NULL 
          String nullCellValue = rowset.getString(i);// get the designated cell's value 
          System.out.println(nullCellValue); 

         } 
         else 
         { 
          System.out.println("not NULL"+i);// Database does not contain NULL 
          String cellValue = rowset.getString(i);// get the designated cell's value 

          System.out.println(cellValue); 

          /*----- check if the value in cell is Numerical or Categorical [0-9]+-----*/ 
          String regex = "[0-9.]+"; 

           if(cellValue.matches(regex)){ 
            System.out.println("Value is numerical"); 

            String colName = rsmd.getColumnName(i); 
            System.out.println(colName); 

            CalcNumericValMean(colName , tableName); 

          } 
          else{ 
           System.out.println("Value is categorical"); 
          } 
         } 
        } 
      } 

      //Clean-up environment 
      rs.close(); 
      stmt.close(); 
      //connection.commit(); 
      closeConnection(); 

     }/*catch (SQLException ex) { 
      try { 
       connection.rollback(); 
       System.out.println("RollBack performed"); 
       closeConnection(); 
       } catch (Exception e1) { 
       System.out.println("RollBack failed"); 
       } 
      } */ 
     catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

} 
// Function to Calculate Mean of Numeric Values 
public void CalcNumericValMean(String colName , String tableName){ 

    Statement st = null; 
    String sql = null; 
    String average = null; 

    try{ 

     setConnection(); 
     //connection.setAutoCommit(false); 
     st = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, 
       ResultSet.CONCUR_UPDATABLE); 

      sql = "SELECT AVG(" + colName + ") FROM " + tableName; 
      System.out.println("sql :" + sql); 

      ResultSet rset = st.executeQuery(sql); 
      CachedRowSet rowset = new CachedRowSetImpl(); 
      rowset.populate(rset); 

      int i = 1; 
      while(rowset.next()) { // check if a result was returned 
        average = rowset.getString(i); // get your result 
        System.out.println("average is : " + average); 
        i++; 
       } 

     //Clean-up environment 
     rset.close(); 
     st.close(); 
     //connection.commit(); 
     closeConnection(); 

    } 
    catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
+0

您有什麼問題? – Abdelhak

+0

在更新列值中的平均數值的空值....給一些建議如何做...... ......? –

+0

我沒有在你的代碼中找到任何UPDATE語句。你嘗試過:'「UPDATE」+ tableName +「SET colName =」+ avarage +「WHERE」+ colName +「IS NULL」'? –

回答

0

也許你應該試試你的表模型的實現? 。表模型覆蓋包含public void setValueAt(Object value,int row,int col),您可以在表中的特定列上進行橢圓和數學運算

+0

你能解釋一下嗎?怎麼做? 。我不熟悉Table Model。 –

+0

你可以通過閱讀Jtable教程獲得更多信息。這非常簡單,涵蓋了JTable需要的大部分主題https://docs.oracle.com/javase/tutorial/uiswing/components/table.html –

相關問題