Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package DAO;
- import java.sql.*;
- import java.util.HashMap;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- import bean.BookBean;
- public class BookDAO {
- //Default constructor for the barebones DAO
- public BookDAO() throws ClassNotFoundException
- {
- Class.forName("com.mysql.jdbc.Driver");
- }
- /*This method will select * from the entire Book table
- *
- * It returns a resultSet that you are responsible for handling right now*/
- public HashMap<String, BookBean> selectAllBook () throws SQLException
- {
- Connection con = DriverManager.getConnection("jdbc:mysql://cashew.kozow.com:3306/ProjectDB", "projectUser", "projectPass");
- PreparedStatement p = con.prepareStatement("SELECT * FROM Book");
- ResultSet r = p.executeQuery();
- HashMap<String, BookBean> resultMap = new HashMap<String, BookBean>();
- while (r.next()) {
- String title = r.getString("TITLE").trim();
- String bid = r.getString("BID").trim();
- int price= r.getInt("PRICE");
- String authors = r.getString("AUTHORS").trim();
- String category = r.getString("CATEGORY").trim();
- String url = r.getString("URL").trim();
- resultMap.put(bid, new BookBean(bid, title, price, authors, category, url));
- }
- return resultMap;
- }
- public HashMap<String, BookBean> selectBooksWithTitle(String query_title) throws SQLException
- {
- //TODO: try-catch block instead of throws -- for other methods in this class too
- Connection con = DriverManager.getConnection("jdbc:mysql://cashew.kozow.com:3306/ProjectDB", "projectUser", "projectPass");
- String query = "SELECT * FROM Book WHERE TITLE = ?";
- PreparedStatement p = null;
- ResultSet r = null;
- HashMap<String, BookBean> resultMap = new HashMap<String, BookBean>();
- con.prepareStatement("SELECT * FROM Book WHERE TITLE LIKE ?");
- try {
- p = con.prepareStatement(query);
- p.setString(1, query_title + "%");
- // execute insert SQL statement
- r = p.executeQuery();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- while (r.next()){
- String title = r.getString("TITLE").trim();
- String bid = r.getString("BID").trim();
- int price= r.getInt("PRICE");
- String authors = r.getString("AUTHORS").trim();
- String category = r.getString("CATEGORY").trim();
- String url = r.getString("URL").trim();
- resultMap.put(bid, new BookBean(bid, title, price, authors, category, url));
- }
- r.close();
- p.close();
- con.close();
- return resultMap;
- }
- public HashMap<String, BookBean> selectBooksWithCategory(String category)
- {
- PreparedStatement p = null;
- String query = "SELECT * FROM Book WHERE category = ?";
- HashMap<String, BookBean> resultMap = new HashMap<String, BookBean>();
- try {
- Connection con = DriverManager.getConnection("jdbc:mysql://cashew.kozow.com:3306/ProjectDB", "projectUser", "projectPass");
- p = con.prepareStatement(query);
- p.setString(1, category);
- ResultSet r = p.executeQuery();
- while (r.next()){
- String title = r.getString("TITLE").trim();
- String bid = r.getString("BID").trim();
- int price= r.getInt("PRICE");
- String authors = r.getString("AUTHORS").trim();
- String category2 = r.getString("CATEGORY").trim();
- String url = r.getString("URL").trim();
- resultMap.put(bid, new BookBean(bid, title, price, authors, category2, url));
- }
- r.close();
- p.close();
- con.close();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- return resultMap;
- }
- public HashMap<String, BookBean> selectBooksWithID(String query_ID) throws SQLException
- {
- //TODO: try-catch block instead of throws -- for other methods in this class too
- Connection con = DriverManager.getConnection("jdbc:mysql://cashew.kozow.com:3306/ProjectDB", "projectUser", "projectPass");
- String query = "SELECT * FROM Book WHERE TITLE LIKE ?";
- PreparedStatement p = null;
- ResultSet r = null;
- HashMap<String, BookBean> resultMap = new HashMap<String, BookBean>();
- con.prepareStatement(query);
- try {
- p = con.prepareStatement(query);
- p.setString(1, query_ID + "%");
- // execute insert SQL statement
- r = p.executeQuery();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- while (r.next()){
- String title = r.getString("TITLE").trim();
- String bid = r.getString("BID").trim();
- int price= r.getInt("PRICE");
- String authors = r.getString("AUTHORS").trim();
- String category = r.getString("CATEGORY").trim();
- //String url = r.getString("URL").trim();
- resultMap.put(bid, new BookBean(bid, title, price, authors, category,""));
- }
- r.close();
- p.close();
- con.close();
- return resultMap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement