筛选对应于 java 中特定列值的行

Filter rows corresponding to particular column value in java

我在 java 中创建了一个函数,通过它我检索 table 第二列的一些值,这些值接近 ten.The 代码的倍数,因为它是

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() 但 I想要在浏览器上打印它并检索与此 rowValue.

检索到的值对应的行

由于您的值打印在控制台上,您可以将这些值放入列表等集合中,或者如果要显示 key/value 对,可以将其放入 Map 中。 取一个整数并分配要显示的行的行号,然后使用

 ResultSet.absolute(int)

在这一行添加List中的值或放入Map中

将这些值放入您的 for (int i1 = 0; i1 < vals.length; i1++) loop.