/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package function.database;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import object.UserApp;

/**
 *
 * @author Administrator
 */
public class SignInProcess {
    public static UserApp sign(java.sql.Connection connection, String username, String password){
        String sql = "SELECT * FROM tai_khoan WHERE username=? AND password=?;";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            return connectionToUserApp(preparedStatement.executeQuery());
        } catch (SQLException ex) {
        }
        return null;
    }
    
    private static UserApp connectionToUserApp(ResultSet resultSet){
        try {
            if(resultSet.next()){
                UserApp userApp = new UserApp();
                userApp.setUsername(resultSet.getString("username"));
                userApp.setPassword(resultSet.getString("password"));
                userApp.setName(resultSet.getString("name"));
                userApp.setBirthday(resultSet.getString("birthday"));
                userApp.setAndress(resultSet.getString("andress"));
                userApp.setPhone(resultSet.getString("phone"));
                userApp.setStatus(resultSet.getInt("status")==1);
                userApp.setRoles(resultSet.getInt("roles"));
                return  userApp;
            }
        } catch (SQLException ex) {
        }
        return null;
    }
}
