Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
194 additions
and
8 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package br.com.faculdadeidez.java.db; | ||
|
||
public class Pessoa { | ||
|
||
private Long id; | ||
private String nome; | ||
private String email; | ||
private String endereco; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getNome() { | ||
return nome; | ||
} | ||
|
||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getEndereco() { | ||
return endereco; | ||
} | ||
|
||
public void setEndereco(String endereco) { | ||
this.endereco = endereco; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/br/com/faculdadeidez/java/db/RepositorioDePessoas.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package br.com.faculdadeidez.java.db; | ||
|
||
import java.util.List; | ||
|
||
public interface RepositorioDePessoas { | ||
|
||
public void adicionar( Pessoa p ); | ||
public List<Pessoa> listar(); | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
src/br/com/faculdadeidez/java/db/RepositorioDePessoasJDBC.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package br.com.faculdadeidez.java.db; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import br.com.faculdadeidez.java.configuracao.IOUtils; | ||
|
||
public class RepositorioDePessoasJDBC implements RepositorioDePessoas { | ||
|
||
static { | ||
try { | ||
Class.forName("org.h2.Driver"); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
protected Connection getConnection() { | ||
try { | ||
return DriverManager.getConnection("jdbc:h2:pessoas_idez"); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void adicionar(Pessoa p) { | ||
|
||
Connection c = this.getConnection(); | ||
|
||
try { | ||
|
||
PreparedStatement s = c.prepareStatement( | ||
"INSERT INTO pessoas ( nome, email, endereco ) VALUES ( ?, ?, ? )"); | ||
s.setString( 1, p.getNome() ); | ||
s.setString(2, p.getEmail()); | ||
s.setString( 3 , p.getEndereco()); | ||
|
||
s.executeUpdate(); | ||
|
||
} catch ( SQLException e ) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
IOUtils.forceClose(c); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public List<Pessoa> listar() { | ||
|
||
List<Pessoa> pessoas = new ArrayList<Pessoa>(); | ||
Connection c = this.getConnection(); | ||
|
||
try { | ||
|
||
PreparedStatement s = c.prepareStatement( "SELECT * FROM pessoas" ); | ||
|
||
ResultSet resultSet = s.executeQuery(); | ||
|
||
while ( resultSet.next() ) { | ||
Pessoa p = new Pessoa(); | ||
p.setId( resultSet.getLong( "id" ) ); | ||
p.setNome( resultSet.getString("nome") ); | ||
p.setEmail(resultSet.getString("email") ); | ||
p.setEndereco( resultSet.getString("endereco") ); | ||
|
||
pessoas.add(p); | ||
} | ||
|
||
} catch ( SQLException e ) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
IOUtils.forceClose( c ); | ||
} | ||
|
||
return pessoas; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/br/com/faculdadeidez/java/db/RepositorioDePessoasTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package br.com.faculdadeidez.java.db; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class RepositorioDePessoasTest { | ||
|
||
@Test | ||
public void testAdicionar() { | ||
|
||
Pessoa p = new Pessoa(); | ||
p.setNome( "José " + System.currentTimeMillis() ); | ||
p.setEmail( "jose@gmail.com" ); | ||
p.setEndereco( "Meu endereço" ); | ||
|
||
RepositorioDePessoas repo = new RepositorioDePessoasJDBC(); | ||
|
||
repo.adicionar(p); | ||
|
||
List<Pessoa> pessoas = repo.listar(); | ||
|
||
Pessoa ultima = pessoas.get( pessoas.size() - 1 ); | ||
|
||
Assert.assertEquals( p.getNome(), ultima.getNome() ); | ||
|
||
} | ||
|
||
} |
10 changes: 9 additions & 1 deletion
10
src/br/com/faculdadeidez/java/pagamentos/ServicoDePagamentoTest.java
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