apuntes:dao
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| apuntes:dao [2023/10/21 11:44] – [Patrón DAO] Santiago Faci | apuntes:dao [2023/10/21 11:47] (current) – [Patrón DAO] Santiago Faci | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| public class Database { | public class Database { | ||
| - | | + | |
| - | | + | |
| + | try { | ||
| + | Class.forName(MYSQL_DRIVER); | ||
| + | connection = DriverManager.getConnection(MYSQL_URL, | ||
| + | System.out.println(" | ||
| + | } catch (ClassNotFoundException cnfe) { | ||
| + | System.out.println(" | ||
| + | cnfe.printStackTrace(); | ||
| + | } catch (SQLException sqle) { | ||
| + | System.out.println(" | ||
| + | sqle.printStackTrace(); | ||
| + | } | ||
| + | |||
| + | return connection; | ||
| + | } | ||
| + | |||
| + | public void close() { | ||
| + | try { | ||
| + | connection.close(); | ||
| + | } catch (SQLException sqle) { | ||
| + | System.out.println(" | ||
| + | sqle.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <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 { | try { | ||
| - | | + | |
| - | | + | |
| - | | + | |
| - | } catch (ClassNotFoundException cnfe) { | + | Book book = new Book(); |
| - | | + | book.setTitle(resultSet.getString("title")); |
| - | cnfe.printStackTrace(); | + | |
| + | book.setPublisher(resultSet.getString("publisher")); | ||
| + | books.add(book); | ||
| + | } | ||
| } catch (SQLException sqle) { | } catch (SQLException sqle) { | ||
| System.out.println(" | System.out.println(" | ||
| Line 21: | Line 110: | ||
| } | } | ||
| - | return | + | return |
| } | } | ||
| - | public | + | public |
| + | String sql = " | ||
| + | Book book = null; | ||
| try { | try { | ||
| - | connection.close(); | + | |
| + | 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) { | } catch (SQLException sqle) { | ||
| System.out.println(" | System.out.println(" | ||
| sqle.printStackTrace(); | sqle.printStackTrace(); | ||
| } | } | ||
| + | |||
| + | return book; | ||
| } | } | ||
| } | } | ||
apuntes/dao.1697888664.txt.gz · Last modified: by Santiago Faci
