Guest User

Untitled

a guest
May 7th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package dbHelpers;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11.  
  12. import model.Product;
  13. import model.ShoppingCart;
  14. import model.User;
  15.  
  16. /**
  17. * @author timothyeber
  18. *
  19. */
  20. public class ReadShoppingCartQuery {
  21.  
  22. private Connection connection;
  23. private ResultSet results;
  24.  
  25. public ReadShoppingCartQuery(String dbName, String uName, String pwd) {
  26. String url = "jdbc:mysql://localhost:3306/" + dbName;
  27.  
  28. //set up driver
  29. try {
  30. Class.forName("com.mysql.jdbc.Driver").newInstance();
  31. this.connection = DriverManager.getConnection(url, uName, pwd);
  32. } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }
  36. }
  37. //use usernum as a paramater ***
  38. public void doReadShoppingCart(User user) {
  39. String query = "SELECT * FROM ShoppingCart WHERE usernum = ?";
  40.  
  41. try {
  42. PreparedStatement ps = this.connection.prepareStatement(query);
  43. ps.setInt(1, user.getUsernum());
  44. this.results = ps.executeQuery();
  45. } catch (SQLException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49.  
  50. }
  51.  
  52. public void doReadShoppingCartUpdate(User user, ShoppingCart cart) {
  53. String query = "SELECT * FROM ShoppingCart WHERE usernum = ? and Product_productID = ?";
  54. try {
  55. PreparedStatement ps = this.connection.prepareStatement(query);
  56. ps.setInt(1, user.getUsernum());
  57. ps.setInt(2, cart.getProductID());
  58. } catch (SQLException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public String getHTMLTable(ShoppingCart cart) {
  65. String table = "";
  66. table += "<table border=1>";
  67. table += "<tr>";
  68. table += "<td>";
  69. table += "Product Name";
  70. table += "</td>";
  71. table += "<td>";
  72. table += "Product Qty";
  73. table += "</td>";
  74. table += "<td>";
  75. table += "Product Price";
  76. table += "</td>";
  77. table += "<td>";
  78. table += "Update";
  79. table += "</td>";
  80. table += "<td>";
  81. table += "Delete";
  82. table += "</td>";
  83. table += "</tr>";
  84.  
  85. try {
  86. while(this.results.next()) {
  87. // product.setProductName(this.results.getString("Product_productName"));
  88. // product.setProductPrice(Double.parseDouble(this.results.getString("Product_productPrice")));
  89. // product.setProductInventory(Integer.parseInt(this.results.getString("quantity")));
  90. cart.setUserID(this.results.getString("User_userID"));
  91. cart.setProductID(Integer.parseInt(this.results.getString("Product_productID")));
  92. cart.setProductQty(Integer.parseInt(this.results.getString("quantity")));
  93. cart.setUsernum(Integer.parseInt(this.results.getString("usernum")));
  94. cart.setProductPrice(Double.parseDouble(this.results.getString("Product_productPrice")));
  95. cart.setProductName(this.results.getString("Product_productName"));
  96.  
  97. table += "<form name=deleteForm action=delete value=Delete>";
  98. table += "<tr>";
  99. table += "<td>";
  100. table += cart.getProductName();
  101. table += "</td>";
  102. table += "<td>";
  103. table += cart.getProductQty();
  104. table += "<input type=hidden name=productQty value=" + cart.getProductQty() + ">";
  105. table += "</td>";
  106. table += "<td>";
  107. table += cart.getProductPrice();
  108. table += "<input type=hidden name=productPrice value=" + cart.getProductPrice() + ">";
  109. table += "</td>";
  110. table += "<td>";
  111. table += "<a href=updateForm?usernum=" + cart.getUsernum() + "?productID=" + cart.getProductID() + " >update item</a>";
  112. table += "</td>";
  113. table += "<td>";
  114. table += "<input type=hidden name=productID value=" + cart.getProductID() + ">";
  115. table += "<input type=hidden name=userID vaalue =" + cart.getUserID() + ">";
  116. table += "<input type=submit name=deleteButton value=Delete>";
  117. table += "</td>";
  118. table += "</tr>";
  119. table += "</form>";
  120. }
  121. } catch (SQLException e) {
  122. // TODO Auto-generated catch block
  123. e.printStackTrace();
  124. }
  125. table += "</table>";
  126. return table;
  127. }
  128.  
  129. public String getHTMLUpdateForm(ShoppingCart cart) {
  130. String table = "";
  131. table += "<table border=1>";
  132. table += "<tr>";
  133. table += "<td>";
  134. table += "Product Name";
  135. table += "</td>";
  136. table += "<td>";
  137. table += "Product Qty";
  138. table += "</td>";
  139. table += "<td>";
  140. table += "Product Price";
  141. table += "</td>";
  142. table += "<td>";
  143. table += "Update";
  144. table += "</td>";
  145. table += "</tr>";
  146.  
  147.  
  148.  
  149.  
  150. try {
  151. while(this.results.next()) {
  152. // product.setProductName(this.results.getString("Product_productName"));
  153. // product.setProductPrice(Double.parseDouble(this.results.getString("Product_productPrice")));
  154. // product.setProductInventory(Integer.parseInt(this.results.getString("quantity")));
  155. cart.setUserID(this.results.getString("User_userID"));
  156. cart.setProductID(Integer.parseInt(this.results.getString("Product_productID")));
  157. cart.setProductQty(Integer.parseInt(this.results.getString("quantity")));
  158. cart.setUsernum(Integer.parseInt(this.results.getString("usernum")));
  159. cart.setProductPrice(Double.parseDouble(this.results.getString("Product_productPrice")));
  160. cart.setProductName(this.results.getString("Product_productName"));
  161.  
  162. table += "<form name=updateForm action=update method=post";
  163. table += "<tr>";
  164. table += "<td>";
  165. table += cart.getProductName();
  166. table += "<input type=hidden name=productName value=" + cart.getProductName() + ">";
  167. table += "</td>";
  168. table += "<td>";
  169. table += "<input type=hidden name=oldProductQty value="+ cart.getProductQty() + ">";
  170. table += "<input type=text name=productQty value=" + cart.getProductQty() + ">";
  171. table += "</td>";
  172. table += "<td>";
  173. table += cart.getProductPrice();
  174. table += "<input type=hidden name=productPrice value=" + cart.getProductPrice() + ">";
  175. table += "<input type=hidden name=productID value=" + cart.getProductID() + ">";
  176. table += "<input type=hidden name=usernum value=" + cart.getUsernum() + ">";
  177. table += "<input type=hidden name=userID value=" + cart.getUserID() + ">";
  178. table += "</td>";
  179. table += "<td>";
  180. table += "<input type=submit name=updateButton value=Update>";
  181. table += "</tr>";
  182. table += "</form>";
  183. }
  184. } catch (SQLException e) {
  185. // TODO Auto-generated catch block
  186. e.printStackTrace();
  187. }
  188. table += "</table>";
  189. return table;
  190. }
  191.  
  192. }
Add Comment
Please, Sign In to add comment