apuntes:dao
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| apuntes:dao [2023/10/21 11:45] – [Patrón DAO] Santiago Faci | apuntes:dao [2023/10/21 11:47] (current) – [Patrón DAO] Santiago Faci | ||
|---|---|---|---|
| Line 32: | Line 32: | ||
| } | } | ||
| } | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file java BookDao.java> | ||
| + | public class BookDao { | ||
| + | |||
| + | private Connection connection; | ||
| + | |||
| + | public BookDao(Connection connection) { | ||
| + | this.connection = connection; | ||
| + | } | ||
| + | |||
| + | public void add(Book book) { | ||
| + | String sql = " | ||
| + | try { | ||
| + | PreparedStatement statement = connection.prepareStatement(sql); | ||
| + | statement.setString(1, | ||
| + | statement.setString(2, | ||
| + | statement.setString(3, | ||
| + | statement.executeUpdate(); | ||
| + | } catch (SQLException sqle) { | ||
| + | System.out.println(" | ||
| + | sqle.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public boolean delete(String title) { | ||
| + | String sql = " | ||
| + | try { | ||
| + | PreparedStatement statement = connection.prepareStatement(sql); | ||
| + | statement.setString(1, | ||
| + | int rows = statement.executeUpdate(); | ||
| + | |||
| + | return rows == 1; | ||
| + | } catch (SQLException sqle) { | ||
| + | System.out.println(" | ||
| + | sqle.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return false; | ||
| + | } | ||
| + | |||
| + | public boolean modify(String title, Book book) { | ||
| + | String sql = " | ||
| + | try { | ||
| + | PreparedStatement statement = connection.prepareStatement(sql); | ||
| + | statement.setString(1, | ||
| + | statement.setString(2, | ||
| + | statement.setString(3, | ||
| + | statement.setString(4, | ||
| + | int rows = statement.executeUpdate(); | ||
| + | return rows == 1; | ||
| + | } catch (SQLException sqle) { | ||
| + | | ||
| + | sqle.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return false; | ||
| + | } | ||
| + | |||
| + | public ArrayList< | ||
| + | String sql = " | ||
| + | ArrayList< | ||
| + | try { | ||
| + | PreparedStatement statement = connection.prepareStatement(sql); | ||
| + | ResultSet resultSet = statement.executeQuery(); | ||
| + | while (resultSet.next()) { | ||
| + | Book book = new Book(); | ||
| + | book.setTitle(resultSet.getString(" | ||
| + | book.setAuthor(resultSet.getString(" | ||
| + | book.setPublisher(resultSet.getString(" | ||
| + | books.add(book); | ||
| + | } | ||
| + | } catch (SQLException sqle) { | ||
| + | System.out.println(" | ||
| + | sqle.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return books; | ||
| + | } | ||
| + | |||
| + | public Book findByTitle(String title) { | ||
| + | String sql = " | ||
| + | Book book = null; | ||
| + | |||
| + | try { | ||
| + | PreparedStatement statement = connection.prepareStatement(sql); | ||
| + | statement.setString(1, | ||
| + | ResultSet resultSet = statement.executeQuery(); | ||
| + | if (resultSet.next()) { | ||
| + | book = new Book(); | ||
| + | book.setTitle(resultSet.getString(" | ||
| + | book.setAuthor(resultSet.getString(" | ||
| + | book.setPublisher(resultSet.getString(" | ||
| + | } | ||
| + | } catch (SQLException sqle) { | ||
| + | System.out.println(" | ||
| + | sqle.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return book; | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
apuntes/dao.1697888712.txt.gz · Last modified: by Santiago Faci
