Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercise3Lib_8140058;
- import java.sql.*;
- public class UserDAO_EX3_8140058 {
- private String errorMessages = "";
- private PreparedStatement stmt = null;
- private ResultSet rs = null;
- private final String userQuery = "select username from user_8xxxxxx where username =? and password=?";
- private String email;
- public User authenticateUser(String username, String password) throws Exception {
- DB_EX3_8140058 dataB = new DB_EX3_8140058();
- dataB.open();
- Connection con = dataB.getConnection();
- try {
- stmt = con.prepareStatement(userQuery);
- // replacing the first ? with username and the second ? with
- // password
- stmt.setString(1, username);
- stmt.setString(2, password);
- // execute query
- rs = stmt.executeQuery();
- int counter = 0;
- while (rs.next())
- counter++;
- if (counter == 1) {
- User user = new User(username, password, email);
- stmt.close();
- rs.close();
- return user;
- } else {
- stmt.close();
- rs.close();
- throw new Exception("Wrong username or password");
- }
- } catch () {
- throw new Exception("Error while executing the method");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement