package br.dsc.upe.poli.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.dsc.upe.poli.banco.ConexaoFactory;
import br.dsc.upe.poli.basicas.Login;

public class UsuarioDAO {

	private Connection connection;

	public UsuarioDAO() throws ClassNotFoundException, SQLException {
		this.connection = new ConexaoFactory().getConexao();
	}

		
	
	public boolean  validacao(Login login) throws SQLException {

		boolean validacao = false;
		PreparedStatement stmt = connection
				.prepareStatement("SELECT * FROM validacao_TABLE WHERE login = ?");
		stmt.setString(1, login.getLogin());
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
			if(login.getSenha()==rs.getString("senha") ||login.getSenha().equals(rs.getString("senha")) ){
				validacao = true;
			}	
		}
		stmt.close();
		rs.close();
		return validacao;

	}

	public Login  buscar(String login) throws SQLException {

		PreparedStatement stmt = connection
				.prepareStatement("SELECT * FROM validacao_TABLE WHERE login = ?");
		stmt.setString(1, login);
		ResultSet rs = stmt.executeQuery();
		Login logado = new Login();		
		while (rs.next()) {
			logado.setLogin(rs.getString("login"));
			logado.setSenha(rs.getString("senha"));
			logado.setTipo_usuario(rs.getString("tipo_usuario"));
			
			
		}
		stmt.close();
		rs.close();
		return logado;

	}

	

	public static void main(String[] args) throws ClassNotFoundException, SQLException{
		Login a = new Login();
		a.setLogin("gustmt");
		a.setSenha("jugu1412");
		UsuarioDAO alunodao =  new UsuarioDAO();

		alunodao.validacao(a);

	}
	
}
