2015-08-15 81 views
-1

我正在使用余弦相似度函数来比较用户输入和SQL中的数据之间的值。最高值将被检索并显示。如果与组合框值匹配显示第n个值

但是,k是从comboBox获得的值,它是意味着它们需要实现的硬约束。所以我把它设置成这样:

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

但是这根本没有意义。如果day只有在第九个最高值时才等于k,那么我需要设置到第九个最高值?有什么方法可以解决这个问题吗?

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

任何人都可以帮忙吗? –

+3

考虑改善您的问题,以便更容易理解。这可能需要您做相当多的工作,但这将值得您一段时间。 –

+0

@HovercraftFullOfEels编辑.. –

回答

0

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

在最基本的层面上,可以考虑构建,看起来基本的查询,如:

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

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

相关问题