2015-08-15 64 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(條件,以確保數據匹配輸入)

如果可能的話,這是確保數據在表格之間排列並且與用戶輸入匹配的高性能方式。

相關問題