过滤与java中特定列值对应的行

时间:2015-01-22 05:35:58

标签: java filter sql-server-2012

我在java中创建了一个函数,通过它我可以检索表格第二列的某些值,这些值的值接近10的倍数。它的代码是



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);
}
}
&#13;
&#13;
&#13;

这个 rowValue 正在打印在控制台上,因为我使用过System.out.format()但我想在浏览器上打印并检索行对应于此rowValue 检索到的值。

1 个答案:

答案 0 :(得分:1)

当您的值打印在控制台上时,您可以将这些值带入列表等集合中,或者如果要显示键/值对,则可以将其带入Map中。 取一个整数并指定要显示的行的行号,然后使用

 ResultSet.absolute(int)

在此行添加列表中的值或放入地图

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