Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. public void addAuthor(Author author) {
  2. try(Connection con = DB.sql2o.open()) {
  3. String sql = "INSERT INTO books_authors (book_id, author_id) VALUES (:book_id, :author_id)";
  4. con.createQuery(sql)
  5. .addParameter("author_id", book.getId())
  6. .addParameter("book_id", this.getId())
  7. .executeUpdate();
  8. }
  9. }
  10.  
  11.  
  12. public List<Author> getAuthors() {
  13. try(Connection con = DB.sql2o.open()){
  14. String sql = "SELECT authors.* FROM books JOIN books_authors ON (books.id = books_authors.book_id) JOIN authors ON (books_authors.author_id = authors.id) where books.id= :book_id;";
  15. List<Autho> authors = con.createQuery(sql)
  16. .addParameter("book_id", this.getId())
  17. .executeAndFetch(Author.class);
  18. return books;
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement