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中,然后测试包含。

相关问题