Фильтровать строки, соответствующие определенному значению столбца в Java

1

Я создал функцию в java, через которую я извлекаю некоторые значения второго столбца моей таблицы, которые имеют значения около кратного десяти. Код для него

public static void FindClosestToMultiplesOfTen() {

  int i = 0;
  try {
    con = getConnection();
    stmt = con.createStatement();
    String sql = "select b.LOGTIME, b.beam_current, b.beam_energy,case " +
      "when a.st1_vs1_bag1_onoff=0 then c.st1_vs1_bag1_rb ELSE 0 END as st1_vs1_bag1_rb," +
      "CASE when a.st1_vs1_bag2_onoff=0  then c.st1_vs1_bag2_rb else '0' END as st1_vs1_bag2_rb," +
      "CASE when a.st1_vs1_bag3_onoff=0  then c.st1_vs1_bag3_rb else '0' END as st1_vs1_bag3_rb," +
      "CASE when a.st1_vs1_bag4_onoff=0  then c.st1_vs1_bag4_rb else '0' END as st1_vs1_bag4_rb," +
      "CASE when a.st1_vs1_bag5_onoff=0  then c.st1_vs1_bag5_rb else '0' END as st1_vs1_bag5_rb," +
      "CASE when a.st1_vs1_bag6_onoff=0  then c.st1_vs1_bag6_rb else '0' END as st1_vs1_bag6_rb," +
      "CASE when a.st1_vs1_bag7_onoff=0  then c.st1_vs1_bag7_rb else '0' END as st1_vs1_bag7_rb," +
      "CASE when a.st1_vs1_bag8_onoff=0  then c.st1_vs1_bag8_rb else '0' END as st1_vs1_bag8_rb " +
      "from INDUS2_BDS.dbo.DCCT b INNER JOIN(main_vacuum_analog c inner join main_vacuum_status a on c.logtime=a.logtime)" +
      "ON a.LOGTIME = b.LOGTIME and (b.beam_current like '%9.96' or b.beam_current like '%9.97' or  b.beam_current like '%9.98' or  b.beam_current like '%9.99'  or  b.beam_current like '%0' or b.beam_current like '%_0.01' or  b.beam_current like '%_0.02' or  b.beam_current like '%_0.03' or  b.beam_current like '%_0.04' or  b.beam_current like '%_0.05' or  b.beam_current like '%_0.06')" +
      "and b.logtime between '2014-10-10 07:17:00' and '2014-10-10 08:46:00'";
    stmt.executeQuery(sql);
    rs = stmt.getResultSet();

    while (rs.next()) {
      for (int j = 0; j < 1; j++) {
        vals[i] = rs.getDouble(2);
      }
      i++;
    }
  } catch (Exception e) {
    System.out.println("\nException " + e);
  }
  //  get the max value, and its multiple of ten to get the number of buckets
  double max = Double.MIN_VALUE;
  for (double v: vals) max = Math.max(max, v);
  int bucketCount = 1 + (int)(max / 10);

  //  initialise the buckets array to store the closest values
  double[][] buckets = new double[bucketCount][3];
  for (int i1 = 0; i1 < bucketCount; i1++) {
    // store the current smallest delta in the first element
    buckets[i1][0] = Double.MAX_VALUE;
    // store the current "closest" index in the second element
    buckets[i1][1] = -1d;
    // store the current "closest" value in the third element
    buckets[i1][0] = Double.MAX_VALUE;
  }

  //  iterate the rows
  for (int i1 = 0; i1 < vals.length; i1++) {
    //  get the value from the row
    double v = vals[i1];
    //  get the closest multiple of ten to v
    double mult = getMultipleOfTen(v);
    //  get the absolute distance of v from the multiple of ten
    double delta = Math.abs(mult - v);
    //  get the bucket index based on the value of 'mult'
    int bIdx = (int)(mult / 10d);
    //    test the last known "smallest delta" for this bucket
    if (buckets[bIdx][0] > delta) {
      //  this is closer than the last known "smallest delta"

      buckets[bIdx][2] = v;
    }
  }

  //   print out the result
  System.out.format("    10x row value%n");
  for (int i1 = 0; i1 < buckets.length; i1++) {
    double[] bucket = buckets[i1];

    double rowValue = bucket[2];
    System.out.format(" %.4f%n", rowValue);
  }
}
public static double getMultipleOfTen(double v) {
  return 10d * Math.round(v / 10d);
}
}

Этот rowValue печатается на консоли, так как я использовал System.out.format(), но я хочу напечатать это в браузере и получить строки, соответствующие значениям, полученным этим rowValue.

Теги:
sql-server-2012
filter

1 ответ

1
Лучший ответ

Когда ваши значения будут напечатаны на консоли, вы можете взять эти значения в коллекции, такой как список, или вы можете взять ее на карте, если хотите показать пару ключ/значение. Возьмите целое число и назначьте номер строки этих строк, которые вы хотите отобразить, а затем используйте

 ResultSet.absolute(int)

В этой строке добавьте значения в список или введите в Map

Поместите эти значения в for (int i1 = 0; i1 < vals.length; i1++) loop.

  • 0
    Я использовал карту, и она работала. Я сделал это так же. Спасибо :)

Ещё вопросы

Сообщество Overcoder
Наверх
Меню