Created
July 1, 2015 04:36
-
-
Save fkenjikamei/6dd236206604677cfa34 to your computer and use it in GitHub Desktop.
Classe Login com acesso ao Banco de Dados
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
public class LoginDAO implements ILoginDAO { | |
static Connection conexao; | |
public LoginDAO(Connection conexaoBD) { | |
this.conexao = conexaoBD; | |
} | |
public boolean consultar(String paramUsuario, String paramSenha) throws SQLException { | |
try { | |
PreparedStatement pstmt = conexao.prepareStatement("select * from Login where usuario ='"+paramUsuario+"' and senha ='"+paramSenha+"'"); | |
ResultSet rs = pstmt.executeQuery(); | |
String nome = null; | |
String senha = null; | |
while(rs.next()) { | |
nome = rs.getString("usuario"); | |
senha = rs.getString("senha"); | |
} | |
if(nome != null && senha != null) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} catch(SQLException e) { | |
System.out.println("Erro = "+e.getMessage()); | |
return false; | |
} | |
} | |
//Posso passar uma query que não retorna nada (insert, update, delete) | |
public static boolean executa(String sql){ | |
Statement stmt = null; | |
try{ | |
stmt = conexao.createStatement(); | |
stmt.executeUpdate(sql); } | |
catch(Exception e){ | |
System.out.println("Erro = "+e.getMessage()); | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public ResultSet listar() throws SQLException { | |
try { | |
PreparedStatement pstmt = conexao.prepareStatement("select * from Login"); | |
ResultSet rs = pstmt.executeQuery(); | |
return rs; | |
} catch(SQLException e) { | |
System.out.println("Erro = "+e.getMessage()); | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import java.sql.Connection; | |
import org.junit.Test; | |
import junit.framework.TestCase; | |
public class LoginDAOTest extends TestCase { | |
@Test | |
public void testDeveriaRetornarTrueSeLoginSenhaValidos() throws Exception { | |
Connection conexaoBD; | |
IConexao conexao = new ConexaoMySQL("admin", "admin"); | |
conexaoBD = conexao.getConexao(); | |
LoginDAO login = new LoginDAO(conexaoBD); | |
assertTrue(login.executa("insert into Login(usuario, senha) values ('teste','123atest')")); | |
assertTrue(login.consultar("teste","123atest")); | |
assertTrue(login.executa("delete from Login where usuario = 'teste'")); | |
} | |
@Test | |
public void testDeveriaInserirUsuario() throws Exception { | |
Connection conexaoBD; | |
IConexao conexao = new ConexaoMySQL("admin", "admin"); | |
conexaoBD = conexao.getConexao(); | |
LoginDAO login = new LoginDAO(conexaoBD); | |
assertTrue(login.executa("insert into Login(usuario, senha) values ('aaaa','bbbb')")); | |
assertTrue(login.consultar("aaaa", "bbbb")); | |
assertTrue(login.executa("delete from Login where usuario = 'aaaa'")); | |
} | |
@Test | |
public void testDeveriaExcluirUsuario() throws Exception { | |
Connection conexaoBD; | |
IConexao conexao = new ConexaoMySQL("admin", "admin"); | |
conexaoBD = conexao.getConexao(); | |
LoginDAO login = new LoginDAO(conexaoBD); | |
assertTrue(login.executa("delete from Login where usuario = 'teste'")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment