如果与 comboBox 值匹配,则显示第 n 个最高值

Display nth highest value if matched with comboBox value

我正在使用余弦相似度函数来比较用户输入与 SQL 中的数据之间的值。将检索并显示最高值。

然而,k 是从 comboBox 得到的值,它是硬约束,这意味着它们需要被满足。所以我将其设置为:

在索引 X 中找到的最大值。在显示之前,它会检查 day 是否等于 k。如果不是,它将查看第二高的,依此类推,直到天等于 k。

但这根本没有意义。如果day在第9高的时候才等于k,那我需要设置到第9高的值吗?有什么方法可以解决吗?

private void pick_highest_value_here_and_display(ArrayList<Double> value,
    int k) throws Exception {
  // TODO Auto-generated method stub
  double aa[] = value.stream().mapToDouble(v -> v.doubleValue()).toArray();
  double highest = Double.MIN_VALUE;
  double secHighest = Double.MIN_VALUE;
  int highestIndex = 0;

  for (int i = 0; i < aa.length; i++) {
    if (aa[i] > highest) {
      highest = aa[i];
      highestIndex = i;
    }
  }
  System.out.println("The highest value is " + highest + "");
  System.out.println("It is found at index " + highestIndex + "");
  String sql = "Select Day from menu where ID =?";
  DatabaseConnection db = new DatabaseConnection();
  Connection conn = db.getConnection();
  PreparedStatement ps = conn.prepareStatement(sql);
  ps.setInt(1, highestIndex);
  ResultSet rs = ps.executeQuery();
  if (rs.next()) {
    int aaa = rs.getInt("Day");
    System.out.println(aaa);
    if (aaa == k) // check whether aaa(day) is equal to k (comboBox)
    {
      String sql1 = "Select * from placeseen where ID =?";
      DatabaseConnection db1 = new DatabaseConnection();
      Connection conn1 = db1.getConnection();
      PreparedStatement ps1 = conn1.prepareStatement(sql1);
      ps1.setInt(1, highestIndex);
      ResultSet rs1 = ps1.executeQuery();
      if (rs1.next()) {
        String a = rs1.getString("place1");
        String bbb = rs1.getString("place2");
        Tourism to = new Tourism();
        to.setPlace1(a);
        to.setPlace2(bbb);
        DispDay dc = new DispDay();
        dc.setVisible(true);
      }
      ps1.close();
      rs1.close();
      conn1.close();
    } else // if not equal
    {

      int secIndex = 0;
      for (int i = 0; i < aa.length; i++) {
        if (aa[i] > secHighest) {
          secHighest = aa[i];
          secIndex = i;
        }
      }
      System.out.println("The second highest value is " + secHighest + "");
      System.out.println("It is found at index " + secIndex + "");
      String sql2 = "Select Day from menu where ID =?";
      DatabaseConnection db2 = new DatabaseConnection();
      Connection conn2 = db2.getConnection();
      PreparedStatement ps2 = conn.prepareStatement(sql2);
      ps2.setInt(1, secIndex);
      ResultSet rs2 = ps.executeQuery();
      if (rs2.next()) {
        int de = rs2.getInt("Day");
        System.out.println(de);
        if (de == k) {
          String l = "Select * from placeseen where ID =?";
          DatabaseConnection db3 = new DatabaseConnection();
          Connection conn3 = db3.getConnection();
          PreparedStatement ps3 = conn3.prepareStatement(l);
          ps3.setInt(1, secIndex);
          ResultSet rs3 = ps3.executeQuery();
          if (rs3.next()) {
            String a = rs3.getString("place1");
            String bbb = rs3.getString("place2");
            Tourism to = new Tourism();
            to.setPlace1(a);
            to.setPlace2(bbb);
            DispDay dc = new DispDay();
            dc.setVisible(true);
          }
          ps3.close();
          rs3.close();
          conn3.close();
        }
      }
    }
    ps.close();
    rs.close();
    conn.close();
  }
}

您的代码有点难以理解,听起来您正在尝试获取 "highest" 数据库值(在某种意义上),其值与某些用户输入相匹配。

在最基本的层面上,考虑构建如下所示的基本查询:

SELECT MAX(值列) 从菜单 加入placeseen 开(条件) WHERE(确保数据匹配输入的条件)

如果可能的话,这是一种确保数据在表之间排列并匹配用户输入的高性能方法。