Advertisement
Guest User

Untitled

a guest
Jan 11th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. package Data;
  2.  
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import BusinessLogic.Product;
  6. import java.io.UnsupportedEncodingException;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12.  
  13. import BusinessLogic.User;
  14. import BusinessLogic.Order;
  15. import BusinessLogic.Password;
  16. import java.util.ArrayList;
  17. import java.util.Random;
  18.  
  19.  
  20. public class DataAccessObject {
  21.  
  22. private final DBConnector conn;
  23. Password pass = new Password();
  24.  
  25. public DataAccessObject() throws Exception {
  26. this.conn = new DBConnector();
  27. }
  28.  
  29. public Product getProduct(String productName){
  30. Statement stmt = null;
  31. try {
  32. stmt = conn.getConnection().createStatement();
  33. } catch (SQLException ex) {
  34. Logger.getLogger(DataAccessObject.class.getName()).log(Level.SEVERE, null, ex);
  35. }
  36. String sql = "select * from gsprice where product = '" + productName + "'";
  37. Product product = null;
  38. try {
  39. ResultSet rs = stmt.executeQuery(sql);
  40. if (rs.next()) {
  41. int price = rs.getInt("Price");
  42. String productRetrieved = rs.getString("Product");
  43. product = new Product(productRetrieved, price);
  44. }
  45. } catch (SQLException ex) {
  46. Logger.getLogger(DataAccessObject.class.getName()).log(Level.SEVERE, null, ex);
  47. }
  48. return product;
  49. }
  50.  
  51. public User getUserByName(String username){
  52. Statement stmt = null;
  53. try {
  54. stmt = conn.getConnection().createStatement();
  55. } catch (SQLException ex) {
  56. Logger.getLogger(DataAccessObject.class.getName()).log(Level.SEVERE, null, ex);
  57. }
  58. String sql = "select * from gsusers where username = '" + username + "';";
  59. User user = null;
  60. try {
  61. ResultSet rs = stmt.executeQuery(sql);
  62. if (rs.next()) {
  63. String usernameRetrieved = rs.getString("username");
  64. String passwordRetrieved = rs.getString("password");
  65. String saltRetrieved = rs.getString("salt");
  66. String emailRetrieved = rs.getString("email");
  67. int phoneNoRetrieved = rs.getInt("PhoneNo");
  68.  
  69. user = new User(usernameRetrieved, passwordRetrieved, saltRetrieved, emailRetrieved, phoneNoRetrieved);
  70. }
  71. } catch (SQLException ex) {
  72. Logger.getLogger(DataAccessObject.class.getName()).log(Level.SEVERE, null, ex);
  73. }
  74. return user;
  75. }
  76.  
  77. public User registreUser(String username, String password, String email, int phoneNo) throws SQLException, UnsupportedEncodingException{
  78. Statement stmt = conn.getConnection().createStatement();
  79. pass.getSaltString();
  80. String sql = "INSERT INTO gsusers VALUES ('" + username + "', '" + pass.get_SHA_512_SecurePassword(password, pass.getPasswordSalt()) + "', '" + pass.getPasswordSalt() + "', '" + email + "', '" + phoneNo + "', '0')";
  81. User user = null;
  82. try{
  83. stmt.executeUpdate(sql);
  84. }catch(Exception e){
  85. System.out.println(e);
  86. }
  87. return user;
  88. }
  89.  
  90. public Order addOrder(String username, double width, double height, double glassBasePrice, double glassPrice, String frameType, double framePrice, double totalPrice) throws SQLException, UnsupportedEncodingException{
  91. Statement stmt = conn.getConnection().createStatement();
  92. String sql = "INSERT INTO gsorders (Username, Width, Height, glassBasePrice, glassPrice, frameType, framePrice, totalPrice) VALUES ('" + username + "', '" + width + "', '" + height + "', '" + glassBasePrice + "', '" + glassPrice + "', '" + frameType + "', '" + framePrice + "', '" + totalPrice + "')";
  93. Order order = null;
  94. try{
  95. stmt.executeUpdate(sql);
  96. }catch(Exception e){
  97. System.out.println(e);
  98. }
  99. return order;
  100. }
  101.  
  102. public ArrayList<Order> getOrdersByUser(String username){
  103. Statement stmt = null;
  104. try {
  105. stmt = conn.getConnection().createStatement();
  106. } catch (SQLException ex) {
  107. Logger.getLogger(DataAccessObject.class.getName()).log(Level.SEVERE, null, ex);
  108. }
  109. String sql = "select * from gsorders where username = '" + username + "'";
  110. ArrayList<Order> orders = new ArrayList<Order>();
  111. Order order = null;
  112. try {
  113. ResultSet rs = stmt.executeQuery(sql);
  114. while (rs.next()) {
  115. int orderID = rs.getInt("orderID");
  116. String usernameRetrieved = rs.getString("username");
  117. double width = rs.getDouble("width");
  118. double height = rs.getDouble("height");
  119. double glassBasePrice = rs.getDouble("glassBasePrice");
  120. double glassPrice = rs.getDouble("glassPrice");
  121. String frameType = rs.getString("frameType");
  122. double framePrice = rs.getDouble("framePrice");
  123. double totalPrice = rs.getDouble("totalPrice");
  124. order = new Order(orderID, usernameRetrieved, width, height, glassBasePrice, glassPrice, frameType, framePrice, totalPrice);
  125. orders.add(order);
  126. }
  127. } catch (SQLException ex) {
  128. Logger.getLogger(DataAccessObject.class.getName()).log(Level.SEVERE, null, ex);
  129. }
  130. return orders;
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement