Programación

1º DAM/DAW - Curso 2023-2024

User Tools

Site Tools


apuntes:dao

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
apuntes:dao [2023/10/21 11:45] – [Patrón DAO] Santiago Faciapuntes:dao [2023/10/21 11:47] (current) – [Patrón DAO] Santiago Faci
Line 32: Line 32:
     }     }
   }   }
 +}
 +</file>
 +
 +<file java BookDao.java>
 +public class BookDao {
 +
 +  private Connection connection;
 +
 +  public BookDao(Connection connection) {
 +    this.connection = connection;
 +  }
 +
 +  public void add(Book book) {
 +    String sql = "INSERT INTO books (title, author, publisher) VALUES (?, ?, ?)";
 +    try {
 +      PreparedStatement statement = connection.prepareStatement(sql);
 +      statement.setString(1, book.getTitle());
 +      statement.setString(2, book.getAuthor());
 +      statement.setString(3, book.getPublisher());
 +      statement.executeUpdate();
 +    } catch (SQLException sqle) {
 +      System.out.println("No se ha podido conectar con el servidor de base de datos. Comprueba que los datos son correctos y que el servidor se ha iniciado");
 +      sqle.printStackTrace();
 +    }
 +  }
 +
 +  public boolean delete(String title) {
 +    String sql = "DELETE FROM books WHERE title = ?";
 +    try {
 +      PreparedStatement statement = connection.prepareStatement(sql);
 +      statement.setString(1, title);
 +      int rows = statement.executeUpdate();
 +
 +      return rows == 1;
 +    } catch (SQLException sqle) {
 +      System.out.println("No se ha podido conectar con el servidor de base de datos. Comprueba que los datos son correctos y que el servidor se ha iniciado");
 +      sqle.printStackTrace();
 +    }
 +
 +    return false;
 +  }
 +
 +  public boolean modify(String title, Book book) {
 +    String sql = "UPDATE books SET title = ?, author = ?, publisher = ? WHERE title = ?";
 +    try {
 +      PreparedStatement statement = connection.prepareStatement(sql);
 +      statement.setString(1, book.getTitle());
 +      statement.setString(2, book.getAuthor());
 +      statement.setString(3, book.getPublisher());
 +      statement.setString(4, title);
 +      int rows = statement.executeUpdate();
 +      return rows == 1;
 +    } catch (SQLException sqle) {
 +       System.out.println("No se ha podido conectar con el servidor de base de datos. Comprueba que los datos son correctos y que el servidor se ha iniciado");
 +      sqle.printStackTrace();
 +        }
 +
 +        return false;
 +    }
 +
 +    public ArrayList<Book> findAll() {
 +        String sql = "SELECT * FROM books ORDER BY title";
 +        ArrayList<Book> books = new ArrayList<>();
 +        try {
 +            PreparedStatement statement = connection.prepareStatement(sql);
 +            ResultSet resultSet = statement.executeQuery();
 +            while (resultSet.next()) {
 +                Book book = new Book();
 +                book.setTitle(resultSet.getString("title"));
 +                book.setAuthor(resultSet.getString("author"));
 +                book.setPublisher(resultSet.getString("publisher"));
 +                books.add(book);
 +            }
 +        } catch (SQLException sqle) {
 +            System.out.println("No se ha podido conectar con el servidor de base de datos. Comprueba que los datos son correctos y que el servidor se ha iniciado");
 +            sqle.printStackTrace();
 +        }
 +
 +        return books;
 +    }
 +
 +    public Book findByTitle(String title) {
 +        String sql = "SELECT * FROM books WHERE title = ?";
 +        Book book = null;
 +
 +        try {
 +            PreparedStatement statement = connection.prepareStatement(sql);
 +            statement.setString(1, title);
 +            ResultSet resultSet = statement.executeQuery();
 +            if (resultSet.next()) {
 +                book = new Book();
 +                book.setTitle(resultSet.getString("title"));
 +                book.setAuthor(resultSet.getString("author"));
 +                book.setPublisher(resultSet.getString("publisher"));
 +            }
 +        } catch (SQLException sqle) {
 +            System.out.println("No se ha podido conectar con el servidor de base de datos. Comprueba que los datos son correctos y que el servidor se ha iniciado");
 +            sqle.printStackTrace();
 +        }
 +
 +        return book;
 +    }
 } }
 </file> </file>
apuntes/dao.1697888712.txt.gz · Last modified: 2023/10/21 11:45 by Santiago Faci