如果与组合框的值匹配n次显示最高值 [英] Display nth highest value if matched with comboBox value

查看:152
本文介绍了如果与组合框的值匹配n次显示最高值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

然而, K 组合框所获得的价值,这是硬约束这意味着他们需要满足。所以我将它设置为一样的东西:

在指标值X中发现的最高值。显示之前,它会检查是否一天等于k。如果不是,它会看第二高,依此类推直到一​​天等于k。

但是,这没有任何意义可言。如果一天等于k,只有当它是在第九最高值,然后我需要设置,直到第九最高值?有没有什么方法可以解决呢?

 私人无效pick_highest_value_here_and_display(ArrayList的<双>的价值,
    INT K)抛出异常{
  // TODO自动生成方法存根
  双AA [] = value.stream()mapToDouble(五 - > v.doubleValue())。的toArray();
  双高= Double.MIN_VALUE;
  双secHighest = Double.MIN_VALUE;
  INT highestIndex = 0;  的for(int i = 0; I< aa.length;我++){
    如果(AA [Ⅰ]≥最高){
      最高= AA [我]
      highestIndex = I;
    }
  }
  的System.out.println(最高值+最高+);
  的System.out.println(+ highestIndex +这是在索引中找到);
  SQL字符串=?从菜单,ID =选择日;
  的DatabaseConnection DB =新的DatabaseConnection();
  连接康恩= db.getConnection();
  preparedStatement PS =康恩prepareStatement(SQL)。
  ps.setInt(1,highestIndex);
  结果集RS = ps.executeQuery();
  如果(rs.next()){
    INT AAA = rs.getInt(日);
    的System.out.println(AAA);
    如果(AAA == K)//检查AAA(天)是否等于K(组合框)
    {
      字符串SQL1 =?从哪里placeseen ID =选择*;
      的DatabaseConnection DB1 =新的DatabaseConnection();
      连接CONN1 = db1.getConnection();
      preparedStatement PS1 = CONN1 prepareStatement(SQL1)。
      ps1.setInt(1,highestIndex);
      ResultSet的RS1 = ps1.executeQuery();
      如果(rs1.next()){
        字符串= rs1.getString(PLACE1);
        串BBB = rs1.getString(place2);
        旅游业=新旅游();
        to.setPlace1(一);
        to.setPlace2(BBB);
        DispDay直流=新DispDay();
        dc.setVisible(真);
      }
      ps1.close();
      rs1.close();
      conn1.close();
    信息} else //如果不相等
    {      INT secIndex = 0;
      的for(int i = 0; I< aa.length;我++){
        如果(AA [Ⅰ]≥secHighest){
          secHighest = AA [我]
          secIndex = I;
        }
      }
      的System.out.println(第二个最高价值是+ secHighest +);
      的System.out.println(+ secIndex +这是在索引中找到);
      字符串SQL2 =从菜单,ID选择天=?;
      DB2的DatabaseConnection =新的DatabaseConnection();
      连接CONN2 = db2.getConnection();
      preparedStatement PS2 =康恩prepareStatement(SQL2)。
      ps2.setInt(1,secIndex);
      ResultSet的RS2 = ps.executeQuery();
      如果(rs2.next()){
        INT德= rs2.getInt(日);
        的System.out.println(DE);
        如果(德== K){
          字符串L =?从哪里placeseen ID =选择*;
          的DatabaseConnection DB3 =新的DatabaseConnection();
          连接CONN3 = db3.getConnection();
          preparedStatement PS3 = CONN3 prepareStatement(L)。
          ps3.setInt(1,secIndex);
          ResultSet的RS3 = ps3.executeQuery();
          如果(rs3.next()){
            字符串= rs3.getString(PLACE1);
            串BBB = rs3.getString(place2);
            旅游业=新旅游();
            to.setPlace1(一);
            to.setPlace2(BBB);
            DispDay直流=新DispDay();
            dc.setVisible(真);
          }
          ps3.close();
          rs3.close();
          conn3.close();
        }
      }
    }
    ps.close();
    rs.close();
    conn.close();
  }
}


解决方案

您code是有些难以理解,其值咬它听起来就像你正试图获得最高数据库值(在某种意义上)匹配一些用户输入。

在最基本的层面上,考虑兴建,看起来像一个基本的查询:

选择MAX(值列)
从菜单
JOIN placeseen
ON(条件)
WHERE(条件,以确保数据匹配输入)

如果这是可能的,这是一个高性能的方式,以确保表之间并且还向上数据线用户输入相匹配

I am using cosine similarity function to compare the value between user input and the data in SQL. The highest value will be retrieved and displayed.

However, k is the value getting from comboBox and it is hard constraints which mean they need to be fulfilled. So I have set it to something like:

The highest value found in index X . Before display, it will check whether day is equal to k. If not, it will look at the second highest and so on until day is equal to k.

But this doesn't make sense at all. If day is equal to k only when it is in the ninth highest value, then I need to set until ninth highest value? Is there any method can solve this?

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();
  }
}

解决方案

Your code is somewhat difficult to understand, bit it sounds like you are trying to obtain the "highest" database value (in some sense) whose value matching some user input.

At the most basic level, consider constructing a basic query that looks like:

SELECT MAX(value column) FROM menu JOIN placeseen ON (conditions) WHERE (condition to ensure that data matches input)

If that's possible, it's a high-performance way to ensure that the data lines up between the tables and also matches the user input.

这篇关于如果与组合框的值匹配n次显示最高值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆