Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Work For the lab work you will be creating a program that will search from e

ID: 3817139 • Letter: L

Question


Lab Work For the lab work you will be creating a program that will search from either book id or title. It will then display the results in a JList, the list must display book id, title, price. Then using that information you will enter a book id and a new price to update the table 'inventory setting the price to a new price based off the entered book id. Only connect to the database once at the beginning of the program use the same connection throughout the program. Requirements: 1) When searching off book id only brings up 1 value does not use title. 2) When searching off title it is a wild card so you can search off the and bring up every book having the in the title. 3) The JList must display book.book id, book title, inventory.price. 4) When updating inventory you will enterabook id and a new price. Then click the update button to update the record. 5) Only connect to the database once use the same connection throughout the program. 6) Should look like the example below. 7) Must turn in the Java source file and form file located in your project src folder. I should be able to open the file and add it to my project to test running the program. Book ID Search Title the 1 The best book ever 5.00 3 To the moon and back 356 Book ID: I Update Price New Price

Explanation / Answer

BookSwing.java
------------------------
package chegg.swingandfx;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class BookSwing extends JFrame {
   private static final long serialVersionUID = 1L;
   private GridLayout mainGrid = null;
   private JPanel middlePanel = null;
   private JPanel topPanel = null;
   private JPanel buttomPanel = null;
   private JLabel bookIDLabel = null;
   private JLabel bookIDTitle = null;
   private JTextField bookIdText = null;
   private JTextField bookTitleText = null;
   private JButton searchButton = null;
   private JLabel bookIdLabelforUpdate = null;
   private JLabel bookPriceLabel = null;
   private JTextField bookIdTextForUpdate = null;
   private JTextField bookPriceTextForUpdate = null;
   private JButton updateButton = null;
   DefaultListModel<Book> listModel = null;
   public BookSwing() {
       mainGrid = new GridLayout(3, 1, 5, 5);
       topPanel = new JPanel();
       middlePanel = new JPanel();
       buttomPanel = new JPanel();
       setTitle("Infix To Postfix Expression Evaluation");
       setSize(500, 200);
       setLayout(mainGrid);
       bookIDLabel = new JLabel("Book ID:");
       bookIDTitle = new JLabel("Book Title:");
       bookIdText = new JTextField();
       bookTitleText = new JTextField();
       searchButton = new JButton("Search");
       bookIdLabelforUpdate = new JLabel("Book ID:");
       bookPriceLabel = new JLabel("Book Price");
       bookIdTextForUpdate = new JTextField();
       bookPriceTextForUpdate = new JTextField();
       updateButton = new JButton("Update");
       topPanel.setLayout(new GridLayout(3, 4, 5, 5));
       topPanel.add(bookIDLabel);
       topPanel.add(bookIdText);
       topPanel.add(bookIDTitle);
       topPanel.add(bookTitleText);
       topPanel.add(searchButton);
       JList<Book> bookList;
       listModel = new DefaultListModel<Book>();
      
       // create the list
       bookList = new JList<Book>(listModel);
       add(bookList);
       middlePanel.setLayout(new GridLayout(1, 1));
       middlePanel.add(bookList);
       buttomPanel.setLayout(new GridLayout(3, 4, 5, 5));
       buttomPanel.add(bookIdLabelforUpdate);
       buttomPanel.add(bookIdTextForUpdate);
       buttomPanel.add(bookPriceLabel);
       buttomPanel.add(bookPriceTextForUpdate);
       buttomPanel.add(updateButton);
       add(topPanel);
       add(middlePanel);
       add(buttomPanel);
       searchButton.addActionListener(new SearchActionListener());
       updateButton.addActionListener(new UpdateActionListener());
       setLayout(new GridLayout(4, 1));
       setSize(400, 300);
       setLocationRelativeTo(null);
       setResizable(true);
       setVisible(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   class SearchActionListener implements ActionListener {
       public void actionPerformed(ActionEvent ae) {
           try {
               System.out.println(bookIdText.getText());
               System.out.println(bookTitleText.getText());
               if (bookIdText.getText() != null) {
                   List<Book> books = DataBaseManager.findBookById(
                           DataBaseManager.getConnection(),
                           bookIdText.getText());
                   for (Book book : books) {
                       listModel.addElement(book);
                   }
               } else if (bookTitleText.getText() != null) {
                   DataBaseManager.findBookById(
                           DataBaseManager.getConnection(),
                           bookTitleText.getText());
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
   }
   class UpdateActionListener implements ActionListener {
       public void actionPerformed(ActionEvent ae) {
           try {
               System.out.println(bookIdTextForUpdate.getText());
               System.out.println(bookPriceTextForUpdate.getText());
               if (bookIdTextForUpdate.getText() == null
                       || bookPriceTextForUpdate.getText() == null)
                   return;
               DataBaseManager.updatePriceForBook(
                       DataBaseManager.getConnection(),
                       bookIdTextForUpdate.getText(),
                       Double.parseDouble(bookPriceTextForUpdate.getText()));
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
   }
   public static void main(String[] args) {
       new BookSwing();
   }
}

Book.java
---------------
package chegg.javafx;
import java.util.ArrayList;
public class DataSet<T extends DataSetLiberaryInterface> extends
       ArrayList<T> {
   private static final long serialVersionUID = -429636488183019476L;
   public DataSet(T item) {
   }
   public boolean add(T objToAdd) {
       return super.add(objToAdd);
   }
   public int size() {
       return super.size();
   }
  
   public T get(int index){
       super.get(index);
   }
  
  
   public Book getMinBook(){
  
   Book minBook = null;
   int size = super.size();
   if(size == 0){
       return null;
   }else{
      
   }
   return minBook;
}
     
  
   public Book getMaxBook()
   {
       int size = super.size();
       if(size == 0){
           return null;
       }else{
           Book maxBook = (Book) super.get(0);
           for(int i=0; i toString(super.toArray());
       }
}

DataBaseManager.java
--------------------------------
package chegg.swingandfx;
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.List;
public class DataBaseManager {
   public static Connection getConnection() {
       Connection connection = null;
       try {
           Class.forName("org.postgresql.Driver");
       } catch (ClassNotFoundException e) {
           System.out.println("Where is your PostgreSQL JDBC Driver? "
                   + "Include in your library path!");
           e.printStackTrace();
           return null;
       }
       try {
           connection = DriverManager.getConnection(
                   "jdbc:postgresql://127.0.0.1:5433/postgres", "postgres",
                   "postgres");
       } catch (SQLException e) {
           System.out.println("Connection Failed! Check output console");
           e.printStackTrace();
           return null;
       }
       if (connection != null) {
           System.out.println("You made it, take control your database now!");
       } else {
           System.out.println("Failed to make connection!");
       }
       return connection;
   }
   public static List<Book> findBookById(Connection con, String bookId)
           throws SQLException {
       List<Book> books = null;
       Statement stmt = null;
       String query = "select book_id, book_name, price from chegg_book where book_id = '"
               + bookId + "'";
       try {
           stmt = con.createStatement();
           ResultSet rs = stmt.executeQuery(query);
           books = new ArrayList<Book>();
           while (rs.next()) {
               Book book = new Book(rs.getString("book_id"),
                       rs.getString("book_name"), Double.parseDouble(rs
                               .getString("price")));
               books.add(book);
           }
       } catch (SQLException e) {
           e.printStackTrace();
       } finally {
           if (stmt != null) {
               stmt.close();
           }
       }
       return books;
   }
   public static List<Book> findBookByTitle(Connection con, String bookId)
           throws SQLException {
       List<Book> books = null;
       Statement stmt = null;
       String query = "select book_id, book_name, price from book where book_name = '"
               + bookId + "'";
       try {
           stmt = con.createStatement();
           ResultSet rs = stmt.executeQuery(query);
           books = new ArrayList<Book>();
           while (rs.next()) {
               Book book = new Book(rs.getString("book_id"),
                       rs.getString("book_name"), Double.parseDouble(rs
                               .getString("price")));
               books.add(book);
           }
       } catch (SQLException e) {
           e.printStackTrace();
       } finally {
           if (stmt != null) {
               stmt.close();
               if (con != null) {
                   con.close();
               }
           }
       }
       return books;
   }
   public static void updatePriceForBook(Connection con, String bookId,
           double price) throws SQLException {
       List<Book> books = null;
       Statement stmt = null;
       String query = "update chegg_book set price = '" + price
               + "' where book_id = '" + bookId + "'";
       try {
           stmt = con.createStatement();
           stmt.executeUpdate(query);
           con.commit();
       } catch (SQLException e) {
           e.printStackTrace();
       } finally {
           if (stmt != null) {
               stmt.close();
               if (con != null) {
                   con.close();
               }
           }
       }
   }
   public static void main(String[] args) {
       try {
           DataBaseManager
                   .findBookById(DataBaseManager.getConnection(), "101");
       } catch (SQLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
}

desscription:

------------------

1. main method of BookSwing.java class needs to be executed to run the above GUI code.

2. DataBaseManager.java class is responsible for communicating with database.

3. You have to add driver class in your netbeen project path.

4. i have added postgres database url string , you need to change the same string with mysql specific

Please let me know if you face any dificultie to run the above code or make any understanding on same.

If you are agree with User interface then i can change the same as given in question otherwise functionality to find and udapte the book record is working fine.

Thanks,