示例#1
0
  @Override
  public void updateBook(String oldisbn, Book book) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String date = sdf.format(book.getPubDate());
    this.jdbcTemplate.update(
        new PreparedStatementCreator() {

          @Override
          public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String query =
                "update "
                    + bookTable
                    + " set isbn = ?, title = ?, description = ?, price = ?, publisher = ?, pubDate = ?, edition = ?, pages = ? where isbn = ?";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, book.getIsbn());
            ps.setString(2, book.getTitle());
            ps.setString(3, book.getDescription());
            ps.setDouble(4, book.getPrice());
            ps.setString(5, book.getPublisher());
            ps.setString(6, date);
            ps.setInt(7, book.getEdition());
            ps.setInt(8, book.getPages());
            ps.setString(9, oldisbn);
            return ps;
          }
        });

    this.jdbcTemplate.update(getPSCForRemoving(bookCategoriesTable, "isbn", book.getIsbn()));
    this.jdbcTemplate.update(getPSCForRemoving(bookAuthorTable, "isbn", book.getIsbn()));

    List<Category> categories = book.getCategories();
    for (Category category : categories) {
      this.jdbcTemplate.update(
          getPSCForInsertingBookCategory(book.getIsbn(), category.getCategoryId()));
    }

    List<Author> authors = book.getAuthors();
    for (Author author : authors) {
      this.jdbcTemplate.update(getPSCForInsertingBookAuthor(book.getIsbn(), author.getAuthorID()));
    }
  }
示例#2
0
  @Override
  public void insertBook(Book book) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String date = sdf.format(book.getPubDate());
    this.jdbcTemplate.update(
        new PreparedStatementCreator() {

          @Override
          public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
            String query =
                "insert into "
                    + bookTable
                    + " (isbn, title, description, price, publisher, pubdate, edition, pages) values (?, ?, ?, ?, ?, ?, ?, ?);";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, book.getIsbn());
            ps.setString(2, book.getTitle());
            ps.setString(3, book.getDescription());
            ps.setDouble(4, book.getPrice());
            ps.setString(5, book.getPublisher());
            ps.setString(6, date);
            ps.setInt(7, book.getEdition());
            ps.setInt(8, book.getPages());
            return ps;
          }
        });

    List<Category> categories = book.getCategories();
    for (Category category : categories) {

      this.jdbcTemplate.update(
          getPSCForInsertingBookCategory(book.getIsbn(), category.getCategoryId()));
    }

    List<Author> authors = book.getAuthors();
    for (Author author : authors) {
      this.jdbcTemplate.update(getPSCForInsertingBookAuthor(book.getIsbn(), author.getAuthorID()));
    }
  }