Advertisement
Guest User

Untitled

a guest
Jun 19th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 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. package DAO;
  7.  
  8. import entities.User;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12.  
  13. /**
  14. *
  15. * @author Petru
  16. */
  17. public class UserDAO {
  18. public boolean login(String email, String password){
  19. // return username.equalsIgnoreCase("abc") && password.equalsIgnoreCase("123");
  20. try {
  21. //1
  22. Class.forName("com.mysql.jdbc.Driver");
  23. //2
  24. java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pcstore","root","");
  25. //3
  26. PreparedStatement pr = cn.prepareStatement("SELECT * FROM client WHERE email=? AND password=?");
  27. pr.setString(1, email);
  28. pr.setString(2, password);
  29. //4
  30. ResultSet rs = pr.executeQuery();
  31. //5
  32. if (rs.next()){
  33. User u = new User(rs.getInt("id_client"), rs.getInt("id_type"), rs.getString("name"), rs.getString("email"), rs.getString("password"), rs.getString("address") );
  34. return email.equalsIgnoreCase(u.getEmail()) && password.equalsIgnoreCase(u.getPassword());
  35. }
  36.  
  37. } catch (Exception ex) {
  38. //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
  39. ex.printStackTrace();
  40. }
  41. return false;
  42. }
  43.  
  44. public void addUser(User u) {
  45. try {
  46. //1
  47. Class.forName("com.mysql.jdbc.Driver");
  48. //2
  49. java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pcstore","root","");
  50. //3
  51. PreparedStatement pr = cn.prepareStatement("INSERT INTO client VALUES (NULL,?,?,?,?,?)");
  52. pr.setString(1, u.getName());
  53. pr.setString(2, u.getEmail());
  54. pr.setString(3, u.getPassword());
  55. pr.setString(4, u.getAddress());
  56. pr.setInt(5, 1);
  57. // pr.setInt(4, p.getState());
  58.  
  59. //4
  60. pr.execute();
  61.  
  62. } catch (Exception ex) {
  63. //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
  64. ex.printStackTrace();
  65. }
  66. }
  67.  
  68. public boolean isAdmin(String email) {
  69. try {
  70. //1
  71. Class.forName("com.mysql.jdbc.Driver");
  72. //2
  73. java.sql.Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/pcstore","root","");
  74. //3
  75. PreparedStatement pr = cn.prepareStatement("SELECT * FROM client WHERE email=? AND id_type=2");
  76. pr.setString(1, email);
  77. //4
  78. ResultSet rs = pr.executeQuery();
  79. //5
  80. if (rs.next()){
  81. return true;
  82. }
  83.  
  84. } catch (Exception ex) {
  85. //Logger.getLogger(operation.class.getName()).log(Level.SEVERE, null, ex);
  86. ex.printStackTrace();
  87. }
  88. return false;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement