Guest User

Untitled

a guest
Aug 4th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.20 KB | None | 0 0
  1. capture the wrong id for the checkbox [struts 1.3.8 Always capture the first ID]
  2. <body>
  3. <h1>Customer Cart</h1><h6><p align="left"><a href="logout">logout</a></p></h6>
  4. <!-- include the DB connection -->
  5. <%@ include file="DBconnection.jsp" %>
  6.  
  7. <% String userID = (String) session.getAttribute("userID");
  8. String categoryID = (String) session.getAttribute("categoryID");
  9. String brandID = (String) session.getAttribute("brandID");
  10.  
  11. System.out.println(userID);%>
  12.  
  13. <!-- retrive the brands from DB to populate the select box -->
  14. <% String QueryString = "select cart.cartID, cart.itemID, cart.Qyt, item.itemName, item.itemPrice from cart , item where cart.itemID = item.itemID and customerID=" + userID + ";";
  15. %>
  16.  
  17. <html:form action="/updateCart">
  18.  
  19. <table border="0">
  20. <thead>
  21. <tr>
  22. <td colspan="5">
  23. <span style='color:red'>
  24. <i><html:errors property="checkbox" /></i>
  25. <i><html:errors property="cartQyt" /></i>
  26. </span>
  27. </td>
  28. </tr>
  29. <tr>
  30. <th></th>
  31. <th>Item Name</th>
  32. <th>Item Price</th>
  33. <th>Qyantity</th>
  34. </tr>
  35. </thead>
  36. <tbody>
  37.  
  38. <%
  39. try {
  40. rs = stmt.executeQuery(QueryString);
  41. while (rs.next()) {
  42. int cartID = rs.getInt("cartID");
  43. int cartItemID = rs.getInt("itemID");
  44. String itemName = rs.getString("itemName");
  45. int itemPrice = rs.getInt("itemPrice");
  46. int Qyt = rs.getInt("Qyt");
  47. %>
  48. <tr>
  49. <td><input type="checkbox" name="checkbox" value="<%=cartID%>"/></td>
  50. <td><%=itemName%></td>
  51. <td><%=itemPrice%></td>
  52. <td><input type="text" name="cartQyt" value="<%=Qyt%>" /></td>
  53.  
  54. <td colspan="4">
  55. <input type="hidden" name="cartItemID" value="<%=cartItemID%>"/>
  56. <input type="hidden" name="cartUserID" value="<%=userID%>"/>
  57. <input type="hidden" name="cartItemPrice" value="<%=itemPrice%>"/>
  58. <html:submit property="cartButton" value="Edit"/>
  59. <html:submit property="cartButton" value="Delete"/>
  60. <html:submit property="cartButton" value="Check out"/>
  61. <%
  62. }
  63.  
  64. } catch (Exception e) {
  65. System.out.println("retrieve from DB:" + e);
  66. }
  67. %>
  68. </td>
  69. </tr>
  70. <tr>
  71. <td>
  72.  
  73. </td>
  74. <td>
  75. <a href="userHome.jsp"><input type="button" name="back" value="back to home"/></a>
  76. </td>
  77. </tr>
  78.  
  79. </tbody>
  80. </table>
  81.  
  82.  
  83. </html:form>
  84.  
  85. </body>
  86.  
  87. public class AddToCartForm extends org.apache.struts.action.ActionForm {
  88.  
  89. private int Qyt;
  90. private int itemID;
  91. private int itemQyt;
  92. private int number;
  93. private int cartUserID;
  94. private int cartItemID;
  95. private int cartItemPrice;
  96. private int cartQyt;
  97. private String cartItemName;
  98. private int cartID;
  99. private String cartButton;
  100. private String checkbox;
  101.  
  102. public String getCartButton() {
  103. return cartButton;
  104. }
  105.  
  106. public void setCartButton(String cartButton) {
  107. this.cartButton = cartButton;
  108. }
  109.  
  110. public String getCheckbox() {
  111. return checkbox;
  112. }
  113.  
  114. public void setCheckbox(String checkbox) {
  115. this.checkbox = checkbox;
  116. }
  117.  
  118. public int getCartID() {
  119. return cartID;
  120. }
  121.  
  122. public void setCartID(int cartID) {
  123. this.cartID = cartID;
  124. }
  125.  
  126.  
  127. public int getCartItemID() {
  128. return cartItemID;
  129. }
  130.  
  131. public void setCartItemID(int cartItemID) {
  132. this.cartItemID = cartItemID;
  133. }
  134.  
  135. public String getCartItemName() {
  136. return cartItemName;
  137. }
  138.  
  139. public void setCartItemName(String cartItemName) {
  140. this.cartItemName = cartItemName;
  141. }
  142.  
  143. public int getCartItemPrice() {
  144. return cartItemPrice;
  145. }
  146.  
  147. public void setCartItemPrice(int cartItemPrice) {
  148. this.cartItemPrice = cartItemPrice;
  149. }
  150.  
  151. public int getCartQyt() {
  152. return cartQyt;
  153. }
  154.  
  155. public void setCartQyt(int cartQyt) {
  156. this.cartQyt = cartQyt;
  157. }
  158.  
  159. public int getCartUserID() {
  160. return cartUserID;
  161. }
  162.  
  163. public void setCartUserID(int cartUserID) {
  164. this.cartUserID = cartUserID;
  165. }
  166.  
  167. public int getItemQyt() {
  168. return itemQyt;
  169. }
  170.  
  171. public void setItemQyt(int itemQyt) {
  172. this.itemQyt = itemQyt;
  173. }
  174.  
  175. public int getItemID() {
  176. return itemID;
  177. }
  178.  
  179. public void setItemID(int itemID) {
  180. this.itemID = itemID;
  181. }
  182.  
  183.  
  184.  
  185.  
  186. /**
  187. * @return
  188. */
  189. public int getQyt() {
  190. return Qyt;
  191. }
  192.  
  193. /**
  194. * @param string
  195. */
  196. public void setQyt(int Qyt) {
  197. this.Qyt = Qyt;
  198. }
  199.  
  200. /**
  201. * @return
  202. */
  203. public int getNumber() {
  204. return number;
  205. }
  206.  
  207. /**
  208. * @param i
  209. */
  210. public void setNumber(int number) {
  211. this.number = number;
  212. }
  213.  
  214. /**
  215. *
  216. */
  217. public AddToCartForm() {
  218. super();
  219. // TODO Auto-generated constructor stub
  220. }
  221.  
  222. public class updateCartAction extends org.apache.struts.action.Action {
  223.  
  224. /* forward name="success" path="" */
  225. private static final String SUCCESS = "success";
  226. private static final String FAILURE = "failure";
  227. // Initialize SQL connection equal null
  228. private Connection con = null;
  229. private ResultSet rs = null;
  230. private Statement stmt = null;
  231.  
  232. /**
  233. * This is the action called from the Struts framework.
  234. * @param mapping The ActionMapping used to select this instance.
  235. * @param form The optional ActionForm bean for this request.
  236. * @param request The HTTP Request we are processing.
  237. * @param response The HTTP Response we are processing.
  238. * @throws java.lang.Exception
  239. * @return
  240. */
  241. @Override
  242. public ActionForward execute(ActionMapping mapping, ActionForm form,
  243. HttpServletRequest request, HttpServletResponse response)
  244. throws Exception {
  245.  
  246. //Initilized Action error
  247. ActionErrors errors = new ActionErrors();
  248. // set the session on
  249. HttpSession session = request.getSession(true);
  250. //Call AddToCartForm Instance here
  251. AddToCartForm formBean = (AddToCartForm) form;
  252. // define newQuantity and itemID and orderStatus and Qyt and itemPrice and customerID
  253. int newQuantity = 0;
  254.  
  255. String orderStatus = "Open";
  256. //Initilize Date today = new Date();
  257. java.util.Date now = new java.util.Date();
  258. String DATE_FORMAT = "yyyy-MM-dd";
  259. SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
  260. String strDateNew = sdf.format(now);
  261. // extract user data
  262. //Integer cartUserID = formBean.getCartUserID();
  263. Integer checkbox = formBean.getCartID();
  264. Integer cartItemID = formBean.getCartItemID();
  265. Integer cartQyt = formBean.getCartQyt();
  266. String cartButton = formBean.getCartButton();
  267. Integer cartItemPrice = formBean.getCartItemPrice();
  268. Integer userID = Integer.parseInt(session.getAttribute("userID").toString());
  269.  
  270. // check user data
  271. System.out.println("cart User whose ID is : " + userID + " want to " + cartButton + " the cart its ID is : " + cartItemID + " that contains the item its ID is : " + cartItemID + " with the Qyt :" + cartQyt);
  272.  
  273. //Set up flag message
  274. boolean flag = false;
  275.  
  276. // perform validation
  277. if ((checkbox == 0) || // checkbox parameter does not exist
  278. (checkbox < 1)){ // the user did not select any checkbox
  279. errors.add("checkbox", new ActionMessage("errors.checkbox.required"));
  280. }
  281. // perform validation
  282. if ((cartQyt == 0) || // cartQyt parameter does not exist
  283. (cartQyt < 1)){ // cartQyt parameter is empty
  284. errors.add("cartQyt", new ActionMessage("errors.cartQyt.required"));
  285. }
  286. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  287. saveErrors(request, errors);
  288. //Check- is errors empty
  289. if (errors.isEmpty()) {
  290. flag = true;
  291. } else {
  292. flag = false;
  293. }
  294. if (flag == true) {
  295.  
  296. // start updateCartAction
  297. System.out.println("////////////////////////// Start updateCartAction /////////////////////////");
  298. // Now Start communicate with database
  299. try {
  300. Class.forName("com.mysql.jdbc.Driver").newInstance();
  301. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gsm", "root", "root");
  302.  
  303.  
  304. if (cartButton.equals("Delete")){ //// if the user select delete button
  305.  
  306. try{
  307.  
  308. // delete from cart
  309. PreparedStatement ps = (PreparedStatement) con.prepareStatement("DELETE FROM cart where cartID=?");
  310. ps.setInt(1, checkbox);
  311. //Execute all the above statement
  312. ps.execute();
  313. //Check which cartID was deleted
  314. System.out.println("Deleted from cart table, the cartID: " + checkbox);
  315.  
  316. } catch (Exception e) {
  317. errors.add("SQLDeletingCart", new ActionMessage("errors.SQLDeletingCart"));
  318. System.out.println("ERROR Deleting : Did not delete cart info from the cart : " + e);
  319. }
  320.  
  321. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  322. saveErrors(request, errors);
  323. //Check- is errors empty
  324. if (errors.isEmpty()) {
  325. flag = true;
  326. } else {
  327. flag = false;
  328. }
  329.  
  330. }else if (cartButton.equals("Edit")){ //// if the user select edit button
  331.  
  332. try{
  333.  
  334. // update the cart Qyt
  335. PreparedStatement ps = (PreparedStatement) con.prepareStatement("UPDATE cart SET Qyt=? where cartID=?");
  336. ps.setInt(1, cartQyt);
  337. ps.setInt(2, checkbox);
  338. //Execute all the above statement
  339. ps.execute();
  340. flag = true;
  341. //Check which cartID was deleted
  342. System.out.println("Updated the quantity in the cart: " + cartQyt + " for the cartID: " + checkbox);
  343.  
  344. } catch (Exception e) {
  345. errors.add("SQLUpdatingCart", new ActionMessage("errors.SQLUpdating"));
  346. System.out.println("ERROR Updating : Did not update the Qyt in the cart : " + e);
  347. }
  348.  
  349. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  350. saveErrors(request, errors);
  351. //Check- is errors empty
  352. if (errors.isEmpty()) {
  353. flag = true;
  354. } else {
  355. flag = false;
  356. String UserID = (String)session.getAttribute("userID");
  357. session.setAttribute("userID", UserID);
  358. return mapping.findForward(FAILURE);
  359. }
  360.  
  361. }else if (cartButton.equals("Check out")){ //// if the user select check out button
  362.  
  363. try{
  364.  
  365. try{
  366.  
  367. // get the cart and item info
  368. PreparedStatement ps1 = (PreparedStatement) con.prepareStatement("select cart.itemID, cart.customerID, cart.Qyt, item.itemQyt, item.itemPrice from cart, item where cart.itemID=item.itemID and cart.cartID=?");
  369. ps1.setInt(1, checkbox);
  370. rs = ps1.executeQuery();
  371.  
  372. if (rs.next()) // Succes
  373. {
  374. flag = true;
  375. int itemID = rs.getInt("itemID");
  376. int customerID = rs.getInt("customerID");
  377. int Qyt = rs.getInt("Qyt");
  378. int itemQyt = rs.getInt("itemQyt");
  379. int itemPrice = rs.getInt("itemPrice");
  380. //new quantity
  381. newQuantity = itemQyt - Qyt;
  382. ///// check the data
  383. System.out.println("The Item ID in the cart is: " + itemID + " and its price is : " + itemPrice + " for the customer whose ID is : " + customerID + " and the quantity selected is : " + Qyt + " from the total Qyt which is: " + itemQyt + " and the new Qyt for the item is : " + newQuantity);
  384.  
  385. }
  386.  
  387. flag = false;
  388.  
  389. } catch (Exception e) {
  390. errors.add("SQLSelectingCartInfo", new ActionMessage("errors.SQLSelectingCartInfo"));
  391. System.out.println("ERROR Selecting : Did not get the cart and item info : " + e);
  392. }
  393.  
  394. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  395. saveErrors(request, errors);
  396. //Check- is errors empty
  397. if (errors.isEmpty()) {
  398. flag = true;
  399. } else {
  400. flag = false;
  401. String UserID = (String)session.getAttribute("userID");
  402. session.setAttribute("userID", UserID);
  403. return mapping.findForward(FAILURE);
  404. }
  405.  
  406. try{
  407.  
  408. // update the item Qyt in the item table after checking it out
  409. PreparedStatement ps2 = (PreparedStatement) con.prepareStatement("UPDATE item SET itemQyt=? WHERE itemID=?");
  410. ps2.setInt(1, newQuantity);
  411. ps2.setInt(2, cartItemID);
  412. int updateRows = ps2.executeUpdate();
  413. ps2.close();
  414.  
  415. } catch (Exception e) {
  416. errors.add("SQLUpdatingItem", new ActionMessage("errors.SQLUpdatingItem"));
  417. System.out.println("ERROR Updating : Did not Update the itemQyt in the item table which the itemId is : " + cartItemID + " and the new Qyt is :" + newQuantity + " " + e);
  418. }
  419.  
  420. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  421. saveErrors(request, errors);
  422. //Check- is errors empty
  423. if (errors.isEmpty()) {
  424. flag = true;
  425. } else {
  426. flag = false;
  427. String UserID = (String)session.getAttribute("userID");
  428. session.setAttribute("userID", UserID);
  429. return mapping.findForward(FAILURE);
  430. }
  431.  
  432. try{
  433. // insert the cart info in the order table
  434. PreparedStatement ps3 = (PreparedStatement) con.prepareStatement("INSERT INTO customerOrder(oderStatus, OrderDate, orderQyt, orderTotalPrice, customerID, itemID)"
  435. + " VALUES (?,?,?,?,?,?) ");
  436. ps3.setString(1, orderStatus);
  437. ps3.setString(2, strDateNew);
  438. ps3.setInt(3, cartQyt);
  439. ps3.setInt(4, cartItemPrice);
  440. ps3.setInt(5, userID);
  441. ps3.setInt(6, cartItemID);
  442.  
  443. ///// check the data
  444. System.out.println("The customer whose ID is: " + userID + " and his ordered item ID : " + cartItemID + " with price = " + cartItemPrice + " and quantity = " + cartQyt + " on that fate : " + strDateNew + " and the order status is : " + orderStatus);
  445.  
  446. } catch (Exception e) {
  447. errors.add("SQLInsertingOrder", new ActionMessage("errors.SQLInsertingOrder"));
  448. System.out.println("ERROR Inserting : Did not insert the info in the order : " + e);
  449. }
  450.  
  451. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  452. saveErrors(request, errors);
  453. //Check- is errors empty
  454. if (errors.isEmpty()) {
  455. flag = true;
  456. } else {
  457. flag = false;
  458. String UserID = (String)session.getAttribute("userID");
  459. session.setAttribute("userID", UserID);
  460. return mapping.findForward(FAILURE);
  461. }
  462.  
  463. try{
  464.  
  465. // delete from cart table after check it out
  466. PreparedStatement ps = (PreparedStatement) con.prepareStatement("DELETE FROM cart where cartID=?");
  467. ps.setInt(1, checkbox);
  468. //Execute all the above statement
  469. ps.execute();
  470.  
  471. } catch (Exception e) {
  472. errors.add("SQLDeletingCart", new ActionMessage("errors.SQLDeletingCart"));
  473. System.out.println("ERROR Deleting from cart : Did not delete the order from the cart : " + e);
  474. }
  475.  
  476. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  477. saveErrors(request, errors);
  478. //Check- is errors empty
  479. if (errors.isEmpty()) {
  480. flag = true;
  481. } else {
  482. flag = false;
  483. String UserID = (String)session.getAttribute("userID");
  484. session.setAttribute("userID", UserID);
  485. return mapping.findForward(FAILURE);
  486. }
  487.  
  488. }catch(Exception e){
  489. System.out.println("ERROR : " + e);
  490. }
  491.  
  492. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  493. saveErrors(request, errors);
  494. //Check- is errors empty
  495. if (errors.isEmpty()) {
  496. flag = true;
  497. } else {
  498. flag = false;
  499. String UserID = (String)session.getAttribute("userID");
  500. session.setAttribute("userID", UserID);
  501. return mapping.findForward(FAILURE);
  502. }
  503. }
  504.  
  505. }catch (Exception ex) {
  506. errors.add("SQLException", new ActionMessage("errors.SQLException"));
  507. throw new SQLException(ex.fillInStackTrace());
  508. }
  509.  
  510. //Close SQL server connection
  511. try {
  512. if (con != null) {
  513. con.close();
  514. }
  515. } catch (SQLException e) {
  516. throw new SQLException(e.getSQLState() + e.fillInStackTrace());
  517. }
  518.  
  519. //Save error messages keys into the appropriate HTTP request attribute for use by the <html:errors> tag
  520. saveErrors(request, errors);
  521. //Check- is errors empty
  522. if (errors.isEmpty()) {
  523. flag = true;
  524. } else {
  525. flag = false;
  526. }
  527.  
  528. if(flag) {
  529. String UserID = (String)session.getAttribute("userID");
  530. session.setAttribute("userID", UserID);
  531. return mapping.findForward(SUCCESS);
  532. }else {
  533. String UserID = (String)session.getAttribute("userID");
  534. session.setAttribute("userID", UserID);
  535. return mapping.findForward(FAILURE);
  536. }
  537.  
  538. }
  539.  
  540. return mapping.findForward(FAILURE);
  541. }
  542. }
Add Comment
Please, Sign In to add comment