Guest User

Untitled

a guest
May 4th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. package dbHelpers;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. import model.Product;
  10. import model.User;
  11.  
  12. public class ReadQuery {
  13. // this is going to allow us to establish connection with database
  14. // this will allow us to view all of the items in the data
  15.  
  16. private Connection connection;
  17. private ResultSet results;
  18.  
  19. public ReadQuery(String dbName, String uName, String pwd) {
  20. String url = "jdbc:mysql://localhost:3306/" + dbName;
  21.  
  22. // set up the driver
  23. try {
  24. Class.forName("com.mysql.jdbc.Driver").newInstance();
  25. this.connection = DriverManager.getConnection(url, uName, pwd);
  26. } catch (InstantiationException e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. } catch (IllegalAccessException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. } catch (ClassNotFoundException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. } catch (SQLException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39. }
  40.  
  41. public void doRead() {
  42. String query = "select * from Product";
  43.  
  44. try {
  45. PreparedStatement ps = this.connection.prepareStatement(query);
  46. this.results = ps.executeQuery();
  47. } catch (SQLException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. public String getHTMLTable(User user, User user2) {
  54. String table = "";
  55. table += "<table border=1>";
  56. table += "<tr>";
  57. table += "<td>";
  58. table += "Product Name";
  59. table += "</td>";
  60. table += "<td>";
  61. table += "Product Image";
  62. table += "</td>";
  63. table += "<td>";
  64. table += "Product Qty";
  65. table += "</td>";
  66. table += "<td>";
  67. table += "Product ID";
  68. table += "</td>";
  69. table += "<td>";
  70. table += "Order Qty";
  71. table += "</td>";
  72. table += "<td>";
  73. table += "Add To Cart";
  74. table += "</td>";
  75. table += "</tr>";
  76. try {
  77. while(this.results.next()) {
  78. Product product = new Product();
  79. product.setProductID(this.results.getInt("productID"));
  80. product.setProductName(this.results.getString("productName"));
  81. product.setProductImage(this.results.getString("productImage"));
  82. product.setProductPrice(this.results.getDouble("productPrice"));
  83. product.setProductCategory(this.results.getString("productCategory"));
  84. product.setProductDescription(this.results.getString("productDescription"));
  85. product.setProductInventory(this.results.getInt("productInventory"));
  86.  
  87.  
  88. table += "<form name=AddToCart action=AddToCart method=post>";
  89.  
  90.  
  91. table += "<tr>";
  92. table += "<td>";
  93. table += product.getProductName();
  94. table += "<input type=hidden name=productName value=" + product.getProductName() + ">";
  95. table += "</td>";
  96. table += "<td>";
  97. table += product.getProductImage();
  98. table += "</td>";
  99. table += "<td>";
  100. table += product.getProductPrice();
  101. table += "<input type=hidden name=productPrice value=" + product.getProductPrice() + ">";
  102. table += "</td>";
  103. table += "<td>";
  104. table += product.getProductID();
  105. table += "<input type=hidden name=productID value=" + product.getProductID() + ">";
  106. table += "</td>";
  107. table += "<td>";
  108. table += "<input type=number name=productQty value=0 min=0 max=" + product.getProductInventory() + ">";
  109. //table += "<input type=text name=productQty value=0>";
  110. table += "</td>";
  111. table += "<td>";
  112. table += "<input type=submit name=submit value=Add>";
  113. table += "<input type=hidden name=productName value=" + product.getProductName() + ">";
  114. table += "<input type=hidden name=userID value=" + user.getUserID() + " >";
  115. table += "<input type=hidden name=usernum2 value=" + user2.getUsernum() + ">";
  116.  
  117. // check product against inventory
  118. table += "</td>";
  119. table += "</tr>";
  120.  
  121. table += "</form>";
  122. }
  123. } catch (SQLException e) {
  124. // TODO Auto-generated catch block
  125. e.printStackTrace();
  126. }
  127.  
  128. table += "</table>";
  129. return table;
  130.  
  131. }
  132.  
  133. }
Add Comment
Please, Sign In to add comment