使用MYSQL按类型查找书籍

时间:2015-11-11 19:43:19

标签: java mysql database swing user-interface

我正在使用带有mysql的JFrame GUI,当用户在文本字段中键入他们想要的类型时,尝试让我的程序找到带有“恐怖,喜剧等”的条目。它与我的getAuthorByLastName完美配合,但我似乎无法使用它来处理类型,我认为它与代码的setString(1,genre)部分有关。任何帮助,将不胜感激。谢谢!

selectBookByAuthorLName = connection.prepareStatement("SELECT * FROM Books WHERE LastName = ?");
selectBookByGenre = connection.prepareStatement("SELECT * FROM Books WHERE Genre = ?");
    public List<Book> getBookByAuthorLastName(String name){
    List<Book> results = null;
    ResultSet resultSet = null;

    try{
        selectBookByAuthorLName.setString(1, name);
        resultSet = selectBookByAuthorLName.executeQuery();
        results = new ArrayList<Book>();
        while(resultSet.next()){
            results.add(new Book(
                    resultSet.getString("ISBN"),
                    resultSet.getString("Title"),
                    resultSet.getString("LastName"),
                    resultSet.getString("FirstName"),
                    resultSet.getString("Genre"),
                    resultSet.getDouble("Price")));
            }
    }
    catch(SQLException sqlException){sqlException.printStackTrace();}
    finally{
        try{
            resultSet.close();
        }
        catch(SQLException sqlException){sqlException.printStackTrace(); close();}
    }
    return results;
}

public List<Book> getBookByGenre(String genre){
    List<Book> results = null;
    ResultSet resultSet = null;

    try{
        selectBookByGenre.setString(1, genre);


        resultSet = selectBookByGenre.executeQuery();
        results = new ArrayList<Book>();
        while(resultSet.next()){
            results.add(new Book(
                    resultSet.getString("ISBN"),
                    resultSet.getString("Title"),
                    resultSet.getString("LastName"),
                    resultSet.getString("FirstName"),
                    resultSet.getString("Genre"),
                    resultSet.getDouble("Price")));
            }
    }
    catch(SQLException sqlException){sqlException.printStackTrace();}
    finally{
        try{
            resultSet.close();
        }
        catch(SQLException sqlException){sqlException.printStackTrace(); close();}
    }
    return results;
}

0 个答案:

没有答案