Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package exercise3Lib_8140058;
  2.  
  3. import java.sql.*;
  4.  
  5. public class UserDAO_EX3_8140058 {
  6.  
  7.     private String errorMessages = "";
  8.  
  9.     private PreparedStatement stmt = null;
  10.  
  11.     private ResultSet rs = null;
  12.  
  13.     private final String userQuery = "select username from user_8xxxxxx where username =? and password=?";
  14.  
  15.     private String email;
  16.  
  17.     public User authenticateUser(String username, String password) throws Exception {
  18.  
  19.         DB_EX3_8140058 dataB = new DB_EX3_8140058();
  20.  
  21.         dataB.open();
  22.         Connection con = dataB.getConnection();
  23.  
  24.         try {
  25.  
  26.             stmt = con.prepareStatement(userQuery);
  27.             // replacing the first ? with username and the second ? with
  28.             // password
  29.             stmt.setString(1, username);
  30.             stmt.setString(2, password);
  31.             // execute query
  32.             rs = stmt.executeQuery();
  33.  
  34.             int counter = 0;
  35.  
  36.             while (rs.next())
  37.                 counter++;
  38.  
  39.             if (counter == 1) {
  40.                 User user = new User(username, password, email);
  41.                 stmt.close();
  42.                 rs.close();
  43.                 return user;
  44.             } else {
  45.                 stmt.close();
  46.                 rs.close();
  47.                 throw new Exception("Wrong username or password");
  48.             }
  49.         } catch () {
  50.  
  51.             throw new Exception("Error while executing the method");
  52.  
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement