程序返回先前的信息而不是新信息

时间:2018-04-28 18:57:26

标签: java postgresql arraylist jdbc

我正在实施一个菜单驱动的程序,用户可以根据不同的选项搜索书籍,例如:ISBN,标题等。我遇到的问题是,如果用户选择相同的选项两次,它将返回与前一次运行相同的卖家和价格。

例如,如果用户选择选项1,并输入isbn 18937646282.假设返回以下内容:seller: adrake Price: 20.56。然后,用户再次选择选项1,并输入isbn:87473626262。它应返回以下内容:Seller:aharrison Price 10:20。但是,它返回与第一次运行相同的内容:seller: adrake Price: 20.56

如果我退出程序并重新运行整个程序,程序将正常工作。此外,我打印出了isbn,它正在为每次运行检索正确的isbn,作者和标题。它只是没有检索正确的卖家和价格。任何帮助或见解将不胜感激。下面的代码显示了option1():

List<BookSale> booklist = new ArrayList<>();
public void option1() throws SQLException, IOException
   {
      String sql;
      String sql_;
      String sql_info;
      int option;
      int i;
      int count = 0;


      // asks user to enter ISBN 
      System.out.print("Enter ISBN of the book you are searching for: ");
      isbn = stdin.next();

       //sql statement to select seller and price for user desired ISBN
       sql_ = "SELECT *" + " from BOOK_SALE " + "WHERE isbn = " +
            "'" + isbn + "'" + "and status =" + "'active'";

      // execute query
      result2 = select.executeQuery(sql_);

      // display Seller and Price of the desired ISBN, add book to ArrayList
      while(result2.next())
      {
         BookSale book = new BookSale(listing_no, seller,isbn, condition,price);
         book.setListingNumber(result2.getInt(1));
         book.setSeller(result2.getString(2));
         book.setISBN(result2.getString(3));
         book.setCondition(result2.getString(4));
         book.setPrice(result2.getDouble(5));
         booklist.add(book);
         count++;
      } // while

for (i = 0; i < count; i++) //0
         {
            // display number of the selections
            System.out.println(i + " " + booklist.get(i));
         }// for

在课堂上打印卖家和价格的功能:

@Override
      public String toString(){
         return ". Seller:" + seller + " Price: $" + price;
         }

0 个答案:

没有答案