Example #1
0
 // Retrieve all the books from database
 public List<BookDetails> getAllBook() {
   String sql = "select * from books";
   ArrayList<BookDetails> list = new ArrayList<BookDetails>();
   try {
     getConnection();
     PreparedStatement pstmt = con.prepareStatement(sql);
     ResultSet rs = pstmt.executeQuery();
     while (rs != null && rs.next()) {
       BookDetails book = new BookDetails();
       book.setBookId(rs.getString("id"));
       book.setDescription(rs.getString("description"));
       book.setFirstName(rs.getString("first_name"));
       book.setInventory(rs.getInt("inventory"));
       book.setOnSale(rs.getBoolean("onSale"));
       book.setPrice(rs.getFloat("price"));
       book.setSurname(rs.getString("surname"));
       book.setTitle(rs.getString("title"));
       book.setYear(rs.getInt("calendar_year"));
       list.add(book);
     }
     pstmt.close();
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return list;
 }
Example #2
0
 // Retrieve book details based on bookId, null is returned if book is not found
 public BookDetails getBookDetails(String bookId) {
   String sql = "select * from books where id = ?";
   BookDetails book = null;
   try {
     getConnection();
     PreparedStatement pstmt = con.prepareStatement(sql);
     pstmt.setString(1, bookId);
     ResultSet rs = pstmt.executeQuery();
     if (rs != null && rs.next()) {
       book = new BookDetails();
       book.setBookId(rs.getString("id"));
       book.setDescription(rs.getString("description"));
       book.setFirstName(rs.getString("first_name"));
       book.setInventory(rs.getInt("inventory"));
       book.setOnSale(rs.getBoolean("onSale"));
       book.setPrice(rs.getFloat("price"));
       book.setSurname(rs.getString("surname"));
       book.setTitle(rs.getString("title"));
       book.setYear(rs.getInt("calendar_year"));
     }
     pstmt.close();
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return book;
 }