Advertisement
Guest User

Untitled

a guest
May 11th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. public class UserDao {
  2.     public int save(User user, ServletContext servletContext) {
  3.         int status = 0;
  4.         try{
  5.             Connection con = (Connection) servletContext.getAttribute("databaseCon");
  6.             PreparedStatement ps=con.prepareStatement(
  7.                     "insert into user(name,surname,username,password) values (?,?,?,?)");
  8.             ps.setString(1, user.getName());
  9.             ps.setString(2, user.getSurname());
  10.             ps.setString(3, user.getUsername());
  11.             ps.setString(4, user.getPassword());
  12.  
  13.             status=ps.executeUpdate();
  14.         }catch(Exception ex){ex.printStackTrace();}
  15.  
  16.         return status;
  17.     }
  18.  
  19.     public User validateLogin(String username, String password, Connection connection) {
  20.         User user = new User();
  21.  
  22.         try {
  23.             PreparedStatement ps = connection.prepareStatement(
  24.                     "select * from user where username = ? and password = ?");
  25.             ps.setString(1, username);
  26.             ps.setString(2, password);
  27.  
  28.             ResultSet rs = ps.executeQuery();
  29.  
  30.             if (rs.next()) {
  31.                 user.setId(rs.getInt(1));
  32.                 user.setName(rs.getString(2));
  33.                 user.setSurname(rs.getString(3));
  34.                 user.setUsername(rs.getString(4));
  35.                 user.setPassword(rs.getString(5));
  36.             }
  37.  
  38.         } catch (SQLException e) {
  39.             e.printStackTrace();
  40.             return null;
  41.         }
  42.  
  43.         return user;
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement