Advertisement
Guest User

UserDAO

a guest
Apr 13th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. package com.socialka.model.user;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. import com.socialka.db.DBConnection;
  9.  
  10. public class UserDAO implements IUserDAO{
  11.     private Connection connection;
  12.    
  13.     public UserDAO() throws Exception {
  14.         this.connection = DBConnection.getInstance().getConnection();
  15.     }
  16.  
  17.     @Override
  18.     public int login(User user) throws UserException {
  19.         try {
  20.             Statement statement = this.connection.createStatement();
  21.             ResultSet resultSet =  statement.executeQuery(
  22.                     String.format("select count(*) from users where username = %s and password = %s);",
  23.                         user.getUsername(), user.getPassword()));
  24.            
  25.             if (resultSet.next()) {
  26.                 return resultSet.getInt("id");
  27.             }
  28.            
  29.             throw new UserException("Invalid user name or password");
  30.            
  31.         } catch (SQLException e) {
  32.             e.printStackTrace();
  33.             throw new UserException("DB is not working", e);
  34.         }
  35.  
  36.     }
  37.  
  38.     @Override
  39.     public void register() {
  40.        
  41.     }
  42.    
  43.     public static void main(String[] args) throws Exception {
  44.         UserDAO dao = new UserDAO();
  45.         int id = dao.login(new User("Guluba", "3333"));
  46.        
  47.         System.out.println(id);
  48.        
  49.     }
  50.    
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement