Guest User

Untitled

a guest
May 9th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 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.  
  13.  
  14. /**
  15. * @author timothyeber
  16. *
  17. */
  18. public class DeleteQuery {
  19.  
  20. private Connection connection;
  21. private ResultSet results;
  22. int productQtyDeleted;
  23.  
  24. public DeleteQuery(String dbName, String uname, String pwd){
  25.  
  26. String url = "jdbc:mysql://localhost:3306/"+dbName;
  27.  
  28. try {
  29. Class.forName("com.mysql.jdbc.Driver").newInstance();
  30. this.connection = DriverManager.getConnection(url, uname, pwd);
  31. } catch (InstantiationException | IllegalAccessException
  32. | ClassNotFoundException | SQLException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. public void doReadProductQty(int productID, int usernum) {
  39. String query = "SELECT quantity from ShoppingCart where Product_productID = ? and usernum = ?";
  40. int productQty = 0;
  41. try {
  42. PreparedStatement ps = connection.prepareStatement(query);
  43. ps.setInt(1, productID);
  44. ps.setInt(2, usernum);
  45.  
  46. this.results = ps.executeQuery();
  47.  
  48.  
  49. } catch (SQLException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53.  
  54. }
  55.  
  56. public int doGetProductQty() {
  57. int productQtyDeleted = 0;
  58. try {
  59. while(this.results.next()) {
  60. productQtyDeleted = results.getInt(1);
  61. }
  62. } catch (SQLException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. }
  66.  
  67. return productQtyDeleted;
  68. }
  69.  
  70. public void doUpdateProductQuantity(int productID, int productQtyDeleted) {
  71. String query = "UPDATE Product set productInventory = productInventory + ? where productID = ?";
  72.  
  73. try {
  74. PreparedStatement ps = connection.prepareStatement(query);
  75.  
  76. ps.setInt(1, productQtyDeleted);
  77. ps.setInt(2, productID);
  78.  
  79. ps.executeUpdate();
  80. } catch (SQLException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. }
  85.  
  86. public void doDelete(int productID, int usernum){
  87. // set up a String to hold our query
  88. String query = "delete from ShoppingCart where Product_productID = ? and usernum = ?";
  89.  
  90. // create a preparedstatement using our query string
  91. try {
  92. PreparedStatement ps = connection.prepareStatement(query);
  93.  
  94. // fill in the preparedstatement
  95. ps.setInt(1, productID);
  96. ps.setInt(2, usernum);
  97.  
  98. // execute the query
  99. ps.executeUpdate();
  100.  
  101. } catch (SQLException e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105.  
  106.  
  107.  
  108.  
  109. }
  110.  
  111. }
Add Comment
Please, Sign In to add comment