Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. package DAO;
  2.  
  3. import java.sql.*;
  4. import java.util.HashMap;
  5.  
  6. import javax.naming.InitialContext;
  7. import javax.naming.NamingException;
  8. import javax.sql.DataSource;
  9. import bean.BookBean;
  10.  
  11. public class BookDAO {
  12.  
  13.  
  14. //Default constructor for the barebones DAO
  15. public BookDAO() throws ClassNotFoundException
  16. {
  17.  
  18. Class.forName("com.mysql.jdbc.Driver");
  19. }
  20.  
  21. /*This method will select * from the entire Book table
  22. *
  23. * It returns a resultSet that you are responsible for handling right now*/
  24. public HashMap<String, BookBean> selectAllBook () throws SQLException
  25. {
  26. Connection con = DriverManager.getConnection("jdbc:mysql://cashew.kozow.com:3306/ProjectDB", "projectUser", "projectPass");
  27.  
  28. PreparedStatement p = con.prepareStatement("SELECT * FROM Book");
  29. ResultSet r = p.executeQuery();
  30. HashMap<String, BookBean> resultMap = new HashMap<String, BookBean>();
  31.  
  32. while (r.next()) {
  33. String title = r.getString("TITLE").trim();
  34. String bid = r.getString("BID").trim();
  35. int price= r.getInt("PRICE");
  36. String authors = r.getString("AUTHORS").trim();
  37. String category = r.getString("CATEGORY").trim();
  38. String url = r.getString("URL").trim();
  39.  
  40. resultMap.put(bid, new BookBean(bid, title, price, authors, category, url));
  41. }
  42. return resultMap;
  43. }
  44.  
  45. public HashMap<String, BookBean> selectBooksWithTitle(String query_title) throws SQLException
  46. {
  47. //TODO: try-catch block instead of throws -- for other methods in this class too
  48. Connection con = DriverManager.getConnection("jdbc:mysql://cashew.kozow.com:3306/ProjectDB", "projectUser", "projectPass");
  49. String query = "SELECT * FROM Book WHERE TITLE = ?";
  50.  
  51. PreparedStatement p = null;
  52. ResultSet r = null;
  53. HashMap<String, BookBean> resultMap = new HashMap<String, BookBean>();
  54.  
  55. con.prepareStatement("SELECT * FROM Book WHERE TITLE LIKE ?");
  56. try {
  57. p = con.prepareStatement(query);
  58. p.setString(1, query_title + "%");
  59. // execute insert SQL statement
  60. r = p.executeQuery();
  61. } catch (SQLException e) {
  62. System.out.println(e.getMessage());
  63.  
  64. }
  65. while (r.next()){
  66. String title = r.getString("TITLE").trim();
  67. String bid = r.getString("BID").trim();
  68. int price= r.getInt("PRICE");
  69. String authors = r.getString("AUTHORS").trim();
  70. String category = r.getString("CATEGORY").trim();
  71. String url = r.getString("URL").trim();
  72.  
  73. resultMap.put(bid, new BookBean(bid, title, price, authors, category, url));
  74.  
  75. }
  76. r.close();
  77. p.close();
  78. con.close();
  79. return resultMap;
  80. }
  81.  
  82. public HashMap<String, BookBean> selectBooksWithCategory(String category)
  83. {
  84. PreparedStatement p = null;
  85.  
  86. String query = "SELECT * FROM Book WHERE category = ?";
  87. HashMap<String, BookBean> resultMap = new HashMap<String, BookBean>();
  88.  
  89. try {
  90. Connection con = DriverManager.getConnection("jdbc:mysql://cashew.kozow.com:3306/ProjectDB", "projectUser", "projectPass");
  91. p = con.prepareStatement(query);
  92.  
  93. p.setString(1, category);
  94.  
  95. ResultSet r = p.executeQuery();
  96.  
  97. while (r.next()){
  98. String title = r.getString("TITLE").trim();
  99. String bid = r.getString("BID").trim();
  100. int price= r.getInt("PRICE");
  101. String authors = r.getString("AUTHORS").trim();
  102. String category2 = r.getString("CATEGORY").trim();
  103. String url = r.getString("URL").trim();
  104.  
  105. resultMap.put(bid, new BookBean(bid, title, price, authors, category2, url));
  106.  
  107. }
  108. r.close();
  109. p.close();
  110. con.close();
  111.  
  112. } catch (SQLException e) {
  113. System.out.println(e.getMessage());
  114. }
  115. return resultMap;
  116.  
  117.  
  118. }
  119.  
  120. public HashMap<String, BookBean> selectBooksWithID(String query_ID) throws SQLException
  121. {
  122. //TODO: try-catch block instead of throws -- for other methods in this class too
  123. Connection con = DriverManager.getConnection("jdbc:mysql://cashew.kozow.com:3306/ProjectDB", "projectUser", "projectPass");
  124. String query = "SELECT * FROM Book WHERE TITLE LIKE ?";
  125.  
  126. PreparedStatement p = null;
  127. ResultSet r = null;
  128. HashMap<String, BookBean> resultMap = new HashMap<String, BookBean>();
  129.  
  130. con.prepareStatement(query);
  131. try {
  132. p = con.prepareStatement(query);
  133. p.setString(1, query_ID + "%");
  134. // execute insert SQL statement
  135. r = p.executeQuery();
  136. } catch (SQLException e) {
  137. System.out.println(e.getMessage());
  138.  
  139. }
  140. while (r.next()){
  141. String title = r.getString("TITLE").trim();
  142. String bid = r.getString("BID").trim();
  143. int price= r.getInt("PRICE");
  144. String authors = r.getString("AUTHORS").trim();
  145. String category = r.getString("CATEGORY").trim();
  146. //String url = r.getString("URL").trim();
  147.  
  148. resultMap.put(bid, new BookBean(bid, title, price, authors, category,""));
  149.  
  150. }
  151. r.close();
  152. p.close();
  153. con.close();
  154. return resultMap;
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement