Advertisement
Guest User

Untitled

a guest
Mar 19th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /*CREATE TABLE Product(
  7. Order_num INTEGER,
  8. Customer_id INTEGER,
  9. Product_id INTEGER,
  10. Quality INTEGER,
  11. Shipping_cost INTEGER,
  12. Sales_date DATE(0001-01-01),
  13. Shipping_date DATE(0001-01-01),
  14. PRIMARY KEY(Product_id));
  15. */
  16. package com.myapp.struts;
  17.  
  18. import java.sql.Connection;
  19. import java.sql.DriverManager;
  20. import java.sql.PreparedStatement;
  21. import javax.faces.bean.ManagedBean;
  22. import javax.faces.bean.SessionScoped;
  23.  
  24. /**
  25. *
  26. * @author
  27. */
  28. @ManagedBean(name = "product")
  29. @SessionScoped
  30. public class Product {
  31.  
  32. private Integer orderNum;
  33. private Integer custID;
  34. private Integer prodID;
  35. private Integer quality;
  36. private Integer shippingCost;
  37. private String salesDate;
  38. private String shippingDate;
  39.  
  40. public Product(Integer orderNum, Integer custID, Integer prodID, Integer quality, Integer shippingCost, String salesDate, String shippingDate) {
  41. this.orderNum = orderNum;
  42. this.custID = custID;
  43. this.prodID = prodID;
  44. this.quality = quality;
  45. this.shippingCost = shippingCost;
  46. this.salesDate = salesDate;
  47. this.shippingDate = shippingDate;
  48. }
  49.  
  50. Product() {
  51. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  52. }
  53.  
  54.  
  55. public Integer getOrderNum() {
  56. return orderNum;
  57. }
  58.  
  59. public void setOrderNum(Integer orderNum) {
  60. this.orderNum = orderNum;
  61. }
  62.  
  63. public Integer getCustID() {
  64. return custID;
  65. }
  66.  
  67. public void setCustID(Integer custID) {
  68. this.custID = custID;
  69. }
  70.  
  71. public Integer getProdID() {
  72. return prodID;
  73. }
  74.  
  75. public void setProdID(Integer prodID) {
  76. this.prodID = prodID;
  77. }
  78.  
  79. public Integer getQuality() {
  80. return quality;
  81. }
  82.  
  83. public void setQuality(Integer quality) {
  84. this.quality = quality;
  85. }
  86.  
  87. public Integer getShippingCost() {
  88. return shippingCost;
  89. }
  90.  
  91. public void setShippingCost(Integer shippingCost) {
  92. this.shippingCost = shippingCost;
  93. }
  94.  
  95. public String getSalesDate() {
  96. return salesDate;
  97. }
  98.  
  99. public void setSalesDate(String salesDate) {
  100. this.salesDate = salesDate;
  101. }
  102.  
  103. public String getShippingDate() {
  104. return shippingDate;
  105. }
  106.  
  107. public void setShippingDate(String shippingDate) {
  108. this.shippingDate = shippingDate;
  109. }
  110.  
  111. public String add() {
  112.  
  113. String url = "jdbc:mysql://localhost:3306/mynewdatabase";
  114.  
  115. String username = "root";
  116. String password = "nbuser";
  117.  
  118. PreparedStatement ps = null;
  119. Connection con = null;
  120.  
  121. try {
  122. Class.forName("com.mysql.jdbc.Driver");
  123. con = DriverManager.getConnection(url, username, password);
  124. String sql = "INSERT INTO Product( Order_num, Customer_id, Product_id, Quality, Shipping_cost, Sales_date,"
  125. + "Shipping_date) VALUES(?,?,?,?,?,?,?)";
  126. ps = con.prepareStatement(sql);
  127.  
  128. ps.setInt(1, orderNum);
  129. ps.setInt(2, custID);
  130. ps.setInt(3, prodID);
  131. ps.setInt(4, quality);
  132. ps.setInt(5, shippingCost);
  133. ps.setString(6,salesDate);
  134. ps.setString(7,shippingDate);
  135. ps.executeUpdate();
  136. System.out.println("Data Added Successfully");
  137.  
  138. con.close();
  139. ps.close();
  140. return "response";
  141. } catch (Exception e) {
  142. System.out.println(e);
  143. e.printStackTrace();
  144. return "request";
  145. }
  146.  
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement