Advertisement
Guest User

Untitled

a guest
May 19th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. public class RegisterRequest {
  2.  
  3. private String login;
  4. private String password;
  5. private String fullName;
  6. public String getLogin() {
  7. return login;
  8. }
  9. public void setLogin(String login) {
  10. this.login = login;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. public void setPassword(String password) {
  16. this.password = password;
  17. }
  18. public String getFullName() {
  19. return fullName;
  20. }
  21. public void setFullName(String fullName) {
  22. this.fullName = fullName;
  23. }
  24.  
  25. public boolean checkPossibleLogin(String login) throws NoSuchAlgorithmException, ClassNotFoundException, SQLException{
  26.  
  27. Class.forName("com.mysql.cj.jdbc.Driver");
  28. Connection conn = null;
  29. Statement stmt = null;
  30. conn = DriverManager.getConnection("jdbc:mysql://localhost/phonebook?characterEncoding=UTF-8&useSSL=false","user", "1812");
  31. stmt = conn.createStatement();
  32. String sql = "SELECT login FROM users";
  33. ResultSet rs = stmt.executeQuery(sql);
  34.  
  35. while(rs.next()){
  36. String PossibleLogin = rs.getString("login");
  37. if(login.equals(PossibleLogin)){
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. public boolean createUser(String login, String password, String fullName) throws NoSuchAlgorithmException, ClassNotFoundException, SQLException{
  44.  
  45. if(checkPossibleLogin(login)){
  46. DigestPassword db = new DigestPassword();
  47. String digestedPassword = db.digestPass(password);
  48. Class.forName("com.mysql.cj.jdbc.Driver");
  49. Connection conn = null;
  50. Statement stmt = null;
  51. conn = DriverManager.getConnection("jdbc:mysql://localhost/phonebook?characterEncoding=UTF-8&useSSL=false","user", "1812");
  52. stmt = conn.createStatement();
  53. String sql = "INSERT INTO users VALUES" + "('" + login + "','" + password + "','" + fullName + "');";
  54. stmt.executeUpdate(sql);
  55.  
  56. return true;
  57.  
  58.  
  59. }else{
  60. System.out.println("Sorry, try another login, this already exisits!");
  61.  
  62. return false;
  63. }
  64.  
  65.  
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement