Advertisement
Guest User

dwaadw

a guest
Nov 29th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. package exercise3Lib_8140181;
  2. import java.sql.*;
  3.  
  4. public class UserDAO_EX3_8140181{
  5.     DB_EX3_8140181 db = new DB_EX3_8140181();
  6.     PreparedStatement stmt= null;
  7.     ResultSet rs = null;
  8.     String ErrorMsg="";
  9.     String sql="select * from ismgroup12.user_8140181 where username=? and password=?";
  10.     Connection con= null;
  11.    
  12.  
  13.     public User authenticateUser(String username, String password) throws Exception{
  14.         int i=0;
  15.         String email = "";
  16.  
  17.         try{
  18.             db.open();
  19.             con = db.getConnection();
  20.             stmt = con.prepareStatement(sql);
  21.             stmt.setString(1,username);
  22.             stmt.setString(2,password);
  23.             rs =stmt.executeQuery();
  24.             while(rs.next()){
  25.                 i++;
  26.                 email = rs.getString("email");
  27.             }
  28.             if(i>0){
  29.                 User user= new User(username,password,email);
  30.                 db.close();
  31.                 return user;
  32.             } else {
  33.                 db.close();
  34.                
  35.                 throw new Exception("Wrong username or password");
  36.             }
  37.  
  38.  
  39.         }catch(SQLException e){
  40.            
  41.             throw new Exception("SQL Exception "+e);
  42.  
  43.         }
  44.        
  45.        
  46.  
  47.     }
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement