daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 20th, 2017 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   public void saveUser(String username, String password) {
  2.         try {
  3.             String sql = "INSERT INTO users(username, password) VALUES (?, ?)";
  4.             PreparedStatement prep = this.connection.prepareStatement(sql);
  5.             prep.setString(1, username);
  6.             String hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());
  7.             prep.setString(2, hashedPassword);
  8.             prep.executeUpdate();
  9.             prep.close();
  10.         } catch (SQLException ex) {
  11.             ex.printStackTrace();
  12.         }
  13.     }
  14.  
  15.     public boolean checkUser(String loginUsername, String loginPassword) {
  16.         try {
  17.             String dbPassword;
  18.             String sql = "SELECT * FROM users WHERE username = ?";
  19.             PreparedStatement prep = this.connection.prepareStatement(sql);
  20.             prep.setString(1, loginUsername);
  21.             ResultSet rs = prep.executeQuery();
  22.  
  23.             if (rs.next()) {
  24.                 dbPassword = rs.getString("password");
  25.                 if (BCrypt.checkpw(loginPassword, dbPassword)) {
  26.                     System.out.println("Excisting user");
  27.                     return true;
  28.                 } else {
  29.                     System.out.println("User not found");
  30.                 }
  31.             }
  32.         } catch (SQLException ex) {
  33.             ex.printStackTrace();
  34.         }
  35.         return false;
  36.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top