2017-05-01 28 views
0

這是一個簡單的庫項目。它必須從數據庫加載數據,通過要求用戶根據關鍵字類型進行搜索。 我有兩個班。其中之一是書類。如何在java中使用SQL實現Interface Comparable,Collection.sort()?

package library; 

import java.sql.Date; 

public class Book implements Comparable<Book> { 

String title; 
String author; 
Date date; 
String ISBN; 
String format; 
String publisher; 
float price; 
String[] keywords; 
String[] inputArray; 
String input; 

public Book(String title_param, String author_param, java.util.Date date_param, String ISBN_param, 
     String format_param, String publisher_param, float price_param, String keywords_param) { 

    title = title_param; 
    author = author_param; 
    date = (Date) date_param; 
    ISBN = ISBN_param; 
    format = format_param; 
    publisher = publisher_param; 
    price = price_param; 
    keywords = keywords_param.split(","); 

} 

public void setUserInput(String userIn) { 
    input = userIn; 
} 

private int getRelevance(String userInput) { 
    inputArray = userInput.split(","); 
    int num = 0; 

    for (int i = 0; i != keywords.length; i++) { 
     String in = inputArray[i]; 

     for (int l = 0; l != keywords.length; l++) { 
      if (in.equals(keywords[l])) 
       num++; 
     } 
    } 

    return num; 
} 

public int compareTo(Book o) { 
    if (this.getRelevance(input) > o.getRelevance(input)) { 
     return 1; 
    } else if (this.getRelevance(input) < o.getRelevance(input)) { 
     return -1; 
    } 

    return 0; 
} 
} 

在第二個我想以正確的方式Collection.sort()和的CompareTo()來調用,在某種程度上,它顯示包含這些關鍵字中的至少一個的書籍。但它必須顯示頂部有來自輸入的關鍵字最多的書籍。 收集和比較零件 現在不工作。

package library; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.Date; 
import java.util.Scanner; 

public class LibrarySearch { 
    static ArrayList<Book> books = new ArrayList<Book>(); 
    ArrayList<LibrarySearch> genres = new ArrayList<LibrarySearch>(); 
    static ArrayList<LibrarySearch> keywords = new ArrayList<LibrarySearch>(); 

    public static void main(String[] args) { 
     load_data(); 
    } 

    private static void load_data() { 
     Collections.sort(books, new Comparator<Book>() { 
      @Override 
      public int compare(Book first, Book second) { 
       if (first.compareTo(second) == 1) { 
        return 1; 
       } else if (first.compareTo(second) == -1) { 
        return -1; 
       } 
       return 0; 

      } 
     }); 

     Connection connection = null; 
     Statement statement = null; 

     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "123456"); 
      statement = connection.createStatement(); 
      System.out.println("Choose to search by keywords or genres"); 
      Scanner scanner = new Scanner(System.in); 
      String input = scanner.nextLine(); 

      if (input.equals("keywords")) { 
       System.out.println("Enter your keywords: "); 
       String[] keyWordsInput = scanner.nextLine().split(","); 
       ResultSet result = null; 

       for (int i = 0; i != keyWordsInput.length; i++) { 
        result = statement 
          .executeQuery(" SELECT * FROM book WHERE keywords LIKE '%" + keyWordsInput[i] + "%'"); 
       } 

       while (result.next()) { 
        int id = result.getInt("id"); 
        String title = result.getString("title"); 
        String author = result.getString("author"); 
        Date date = result.getDate("date"); 
        String ISBN = result.getString("ISBN"); 
        String format = result.getString("format"); 
        String publisher = result.getString("publisher"); 
        float price = result.getFloat("price"); 
        String keywords = result.getString("keywords"); 

        System.out.println("ID = " + id); 
        System.out.println("TITLE = " + title); 
        System.out.println("AUTHOR = " + author); 
        System.out.println("DATE = " + date); 
        System.out.println("ISBN = " + ISBN); 
        System.out.println("FORMAT = " + format); 
        System.out.println("PUBLISHER = " + publisher); 
        System.out.println("PRICE = " + price); 
        System.out.println("KEYWORDS = " + keywords); 
        System.out.println("___________________________________________________________________________"); 

        if (title.equals("I,Robot")) { 
         Book new_book = new Book(title, author, date, ISBN, format, publisher, price, keywords); 
         books.add(new_book); 
        } 
        if (title.equals("Catch-22")) { 
         Book new_book1 = new Book(title, author, date, ISBN, format, publisher, price, keywords); 
         books.add(new_book1); 
        } 
        if (title.equals("Pride and Prejudice")) { 
         Book new_book2 = new Book(title, author, date, ISBN, format, publisher, price, keywords); 
         books.add(new_book2); 
        } 
        if (title.equals("Gone with the Wind")) { 
         Book new_book3 = new Book(title, author, date, ISBN, format, publisher, price, keywords); 
         books.add(new_book3); 
        } 

       } 
       result.close(); 
       statement.close(); 
       connection.close(); 

      } else if (input.equals("genres")) { 
       System.out.println("Enter your genres" + ": "); 

       String genresInput = scanner.nextLine(); 
       ResultSet result = statement.executeQuery(
         " SELECT * FROM books_genres JOIN book ON (book.id = books_genres.book_id) JOIN genre ON (genre.id = books_genres.genre_id) WHERE name LIKE '%" 
           + genresInput + "%' "); 
       while (result.next()) { 
        int id = result.getInt("id"); 
        String name = result.getString("name"); 

        int book_id = result.getInt("book_id"); 
        int genre_id = result.getInt("genre_id"); 

        int id1 = result.getInt("id"); 
        String title = result.getString("title"); 
        String author = result.getString("author"); 
        Date date = result.getDate("date"); 
        String ISBN = result.getString("ISBN"); 
        String format = result.getString("format"); 
        String publisher = result.getString("publisher"); 
        float price = result.getFloat("price"); 
        String keywords = result.getString("keywords"); 

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 

        System.out.println("Book ID = " + id1); 
        System.out.println("TITLE = " + title); 
        System.out.println("AUTHOR = " + author); 
        System.out.println("DATE = " + date); 
        System.out.println("ISBN = " + ISBN); 
        System.out.println("FORMAT = " + format); 
        System.out.println("PUBLISHER = " + publisher); 
        System.out.println("PRICE = " + price); 
        System.out.println("KEYWORDS = " + keywords); 

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 

        System.out.println("Genre ID = " + id); 
        System.out.println("Genre Name = " + name); 

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 

        System.out.println("Book ID = " + book_id); 
        System.out.println("Genre ID = " + genre_id); 

       } 

       result.close(); 
       statement.close(); 
       connection.close(); 
      } 

      else { 
       System.out.println("Sorry, wrong command"); 
      } 

     } catch (SQLException ex) { 
      System.out.println("No successful connection"); 
      System.out.println("SQLException: " + ex.getMessage()); 
      System.out.println("SQLState: " + ex.getSQLState()); 
     } 

     catch (ClassNotFoundException x_not_found) { 
      System.out.println("Class not found"); 
     } 
    } 

} 
+0

從它的外觀來看,你知道如何實現'Comparable '自從你的'Book'實現它。你的問題是什麼?問題是什麼?請更詳細一些,否則我們無法幫助你。 – Turing85

+0

它不工作,我猜不能以正確的方式調用它。謝謝 – Geya

+0

「這不起作用。」 - >什麼是「它」?正如我所說:請更詳細。哪些部件正在工作?哪些部件不工作?從查看你的問題,我無法推斷你是否有概念問題,數據庫連接問題,排序問題,其中一些問題還是所有問題。 [MCVE](https://stackoverflow.com/help/mcve)也將不勝感激。 – Turing85

回答

0

首先,因爲getRelevance()方法返回INT,我建議這樣寫的compareTo:

public int compareTo(Book o) { 
    return this.getRelevance(input) - o.getRelevance(input); 
} 

同樣,比較應該是這樣的:

Collections.sort(books, new Comparator<Book>() { 
    @Override 
    public int compare(Book first, Book second) {    
     return first.compareTo(second); 
    } 
}); 

至於調用,看起來就像先排序空列表,然後用搜索結果填充它。我建議將排序部分移到load_data()方法的末尾。

0

一個問題:您正在迭代inputArraykeywords.length次,這可能不正確。不知道這是否是你的問題 - 可能是關鍵字數組比輸入數組短得多。

更大的問題是迭代inputArray的元素的關鍵字。把你的關鍵字放在HashSet中,然後測試包含。