⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 booknewsdao.java

📁 beginJsp2.0外文书籍源代码
💻 JAVA
字号:
package com.wrox.publish.db;import com.wrox.publish.om.Book;import com.wrox.publish.om.BookNews;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Collection;/**  * Data Access Object responsible for reading, writing and deleting * BookNews in the database. */public class BookNewsDAO {        private final Connection conn;        /**      * Creates a new BookNewsDAO with the given database connection.     * @param conn The database connection to use.     */    public BookNewsDAO(Connection conn) {        super();        this.conn = conn;    }        /**     * Creates a new BookNews item in the database.     * @param news The BookNews item to create.     * @throws SQLException If there was a problem accessing the database.     */        public void create(BookNews news) throws SQLException {        PreparedStatement insert = conn.prepareStatement(            "insert into book_news (book, title, body, user, published)" +             "values (?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);        try {            populate(insert, news);            insert.executeUpdate();            ResultSet key = insert.getGeneratedKeys();            if (!key.next()) {                throw new RuntimeException(                    "Can't retrieve generated primary key!");            }            news.setId(key.getLong(1));            key.close();        } finally {            insert.close();        }    }        /**     * Updates a BookNews item in the database.     * @param news The item to update.     * @throws SQLException If there was a problem accessing the database.     * @throws EntityNotFoundException If the item was not found.     */        public void update(BookNews news)     throws SQLException, EntityNotFoundException {        PreparedStatement update = conn.prepareStatement(            "update book_news " +             "set book=?, title=?, body=?, user=?, published=? " +             "where id=?");        try {            populate(update, news);            update.setLong(6, news.getId());            if (update.executeUpdate() != 1) {                throw new EntityNotFoundException(                    "BookNews " + news.getId() + " not found");            }        } finally {            update.close();        }    }        private void populate(PreparedStatement stmt, BookNews news)     throws SQLException {        stmt.setLong(1, news.getBook().getId());        stmt.setString(2, news.getTitle());        stmt.setString(3, news.getBody());        stmt.setString(4, news.getUser());        stmt.setTimestamp(5, news.getPublished());    }    /**     * Removes a BookNews item from the database.     * @param news The BookNews item to remove.     * @throws SQLException If there was a problem accessing the database.     * @throws EntityNotFoundException If the Book was not found.     */        public void remove(BookNews news)     throws SQLException, EntityNotFoundException {        remove(news.getId());    }        /**     * Removes a BookNews item from the database.     * @param id The id (primary key) of the item to remove.     * @throws SQLException If there was a problem accessing the database.     * @throws EntityNotFoundException If the item was not found.     */        public void remove(long id)     throws SQLException, EntityNotFoundException {        PreparedStatement delete = conn.prepareStatement(            "delete from book_news where id=?");        try {            delete.setLong(1, id);            if (delete.executeUpdate() != 1) {                throw new EntityNotFoundException(                    "BookNews " + id + " not found");            }        } finally {            delete.close();        }    }        /**     * Finds a single BookNews item by its id (primary key). This methods      * loads the entire object graph including the parent Book object, but     * not the other dependent BookNews items for that Book.     * @param id The id value to look for     * @throws SQLException If there was a problem accessing the database     * @throws EntityNotFoundException If the BookNews was not found.     * @return The BookNews object     */        public BookNews findById(long id)     throws SQLException, EntityNotFoundException {        BookDAO bookDAO = new BookDAO(conn);        PreparedStatement select = conn.prepareStatement(            "select book, title, body, user, published " +             "from book_news where id=?");        try {            select.setLong(1, id);            ResultSet results = select.executeQuery();            if (!results.next()) {                throw new EntityNotFoundException(                    "BookNews " + id + " not found");            } else {                BookNews news = new BookNews();                news.setId(id);                news.setBook(bookDAO.loadById(results.getLong(1)));                news.setTitle(results.getString(2));                news.setBody(results.getString(3));                news.setUser(results.getString(4));                news.setPublished(results.getTimestamp(5));                return news;            }        } finally {            select.close();        }    }        /**      * Multi-object finder method which finds the BookNews items     * associated with a Book. Used by BookDAO.     * @param book The book to find news items for.     * @return A Collection of BookNews objects.     * @throws SQLException If there was a problem accessing the database     */    public Collection findByBook(Book book) throws SQLException {        PreparedStatement select = conn.prepareStatement(            "select id, title, body, user, published " +             "from book_news where book=?");        try {            select.setLong(1, book.getId());            ResultSet results = select.executeQuery();            Collection items = new ArrayList();            while (results.next()) {                BookNews news = new BookNews();                news.setId(results.getLong(1));                news.setBook(book);                news.setTitle(results.getString(2));                news.setBody(results.getString(3));                news.setUser(results.getString(4));                news.setPublished(results.getTimestamp(5));                items.add(news);            }            return items;        } finally {            select.close();        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -