-
Notifications
You must be signed in to change notification settings - Fork 68
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
0 parents
commit 13e7115
Showing
16,920 changed files
with
825,944 additions
and
0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
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,8 @@ | ||
.idea/ | ||
.settings/ | ||
out/ | ||
target/ | ||
transaction.log | ||
.classpath | ||
.project | ||
*iml |
13 changes: 13 additions & 0 deletions
13
...4-criando-tabelas-e-persistindo-dados-pelo-workbench/Criando banco, tabela e registro.sql
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,13 @@ | ||
create database cadastroevento; | ||
|
||
create table evento ( | ||
id bigint not null auto_increment, | ||
nome varchar(60) not null, | ||
data datetime not null, | ||
primary key(id) | ||
); | ||
|
||
insert into evento (id, nome, data) | ||
values (null, 'Palestra João Silva', sysdate()); | ||
|
||
select * from evento; |
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,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.algaworks</groupId> | ||
<artifactId>entendendo-o-junit</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.5.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
40 changes: 40 additions & 0 deletions
40
01.09-entendendo-o-junit/src/test/java/com/algaworks/junit/EntendendoJUnitTest.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,40 @@ | ||
package com.algaworks.junit; | ||
|
||
import org.junit.*; | ||
|
||
public class EntendendoJUnitTest { | ||
|
||
@BeforeClass | ||
public static void iniciarTestes() { | ||
System.out.println(">>> public static void iniciarTestes() <<<"); | ||
} | ||
|
||
@AfterClass | ||
public static void encerrarTestes() { | ||
System.out.println(">>> public static void encerrarTestes() <<<"); | ||
} | ||
|
||
@Before | ||
public void iniciarTeste() { | ||
System.out.println(">>> public void iniciarTeste() <<<"); | ||
} | ||
|
||
@After | ||
public void encerrarTeste() { | ||
System.out.println(">>> public void encerrarTeste() <<<"); | ||
} | ||
|
||
@Test | ||
public void testandoAlgo() { | ||
String nome = String.format("%s", "Alexandre"); | ||
|
||
Assert.assertEquals("Alexandre", nome); | ||
} | ||
|
||
@Test | ||
public void testandoOutraCoisa() { | ||
String str = String.format("%s", ""); | ||
|
||
Assert.assertTrue(str.isEmpty()); | ||
} | ||
} |
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,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.algaworks</groupId> | ||
<artifactId>cadastro-evento-jdbc</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.16</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.5.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
99 changes: 99 additions & 0 deletions
99
01.10-usando-jdbc-diretamente/src/main/java/com/algaworks/eventos/dao/EventoDAO.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,99 @@ | ||
package com.algaworks.eventos.dao; | ||
|
||
import java.sql.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.algaworks.eventos.model.Evento; | ||
|
||
public class EventoDAO { | ||
|
||
private final Connection connection; | ||
|
||
public EventoDAO(Connection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
public Integer salvar(Evento evento) { | ||
String sql = "insert into evento (id, nome, data) values (null, ?, ?)"; | ||
|
||
try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { | ||
preparedStatement.setString(1, evento.getNome()); | ||
preparedStatement.setDate(2, new Date(evento.getData().getTime())); | ||
|
||
preparedStatement.executeUpdate(); | ||
|
||
try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) { | ||
resultSet.next(); | ||
|
||
return resultSet.getInt(1); | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void atualizar(Evento evento) { | ||
String sql = "update evento set nome = ?, data = ? where id = ?"; | ||
|
||
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { | ||
preparedStatement.setString(1, evento.getNome()); | ||
preparedStatement.setDate(2, new Date(evento.getData().getTime())); | ||
preparedStatement.setInt(3, evento.getId()); | ||
|
||
preparedStatement.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public Evento buscar(Integer id) { | ||
String sql = "select * from evento where id = ?"; | ||
|
||
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { | ||
preparedStatement.setInt(1, id); | ||
|
||
try (ResultSet resultSet = preparedStatement.executeQuery()) { | ||
if(!resultSet.next()) { | ||
return null; | ||
} | ||
|
||
return new Evento(id, resultSet.getString("nome"), | ||
resultSet.getDate("data")); | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public List<Evento> listar() { | ||
String sql = "select * from evento"; | ||
|
||
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { | ||
try (ResultSet resultSet = preparedStatement.executeQuery()) { | ||
List<Evento> eventos = new ArrayList<Evento>(); | ||
|
||
while(resultSet.next()) { | ||
eventos.add(new Evento(resultSet.getInt("id"), | ||
resultSet.getString("nome"), resultSet.getDate("data"))); | ||
} | ||
|
||
return eventos; | ||
} | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void deletar(Integer id) { | ||
String sql = "delete from evento where id = ?"; | ||
|
||
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { | ||
preparedStatement.setInt(1, id); | ||
|
||
preparedStatement.executeUpdate(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
01.10-usando-jdbc-diretamente/src/main/java/com/algaworks/eventos/model/Evento.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,75 @@ | ||
package com.algaworks.eventos.model; | ||
|
||
import java.util.Date; | ||
|
||
public class Evento { | ||
|
||
private Integer id; | ||
|
||
private String nome; | ||
|
||
private Date data; | ||
|
||
public Evento() {} | ||
|
||
public Evento(Integer id, String nome, Date data) { | ||
this.id = id; | ||
this.nome = nome; | ||
this.data = data; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public String getNome() { | ||
return nome; | ||
} | ||
|
||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
|
||
public Date getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Date data) { | ||
this.data = data; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ((id == null) ? 0 : id.hashCode()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null) { | ||
return false; | ||
} | ||
|
||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
Evento other = (Evento) obj; | ||
|
||
if (id == null || other.id == null) { | ||
return false; | ||
} | ||
|
||
return id.equals(other.id); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
01.10-usando-jdbc-diretamente/src/test/java/com/algaworks/eventos/dao/EventoDAOTest.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,67 @@ | ||
package com.algaworks.eventos.dao; | ||
|
||
import org.junit.Assert; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.util.Date; | ||
import java.util.Optional; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import com.algaworks.eventos.dao.EventoDAO; | ||
import com.algaworks.eventos.model.Evento; | ||
|
||
public class EventoDAOTest { | ||
|
||
private static Connection connection; | ||
|
||
@BeforeClass | ||
public static void iniciarClasse() throws SQLException { | ||
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/cadastroevento" + | ||
"?useTimezone=true&serverTimezone=UTC", "root", "1234"); | ||
} | ||
|
||
@AfterClass | ||
public static void encerrarClasse() throws SQLException { | ||
connection.close(); | ||
} | ||
|
||
@Test | ||
public void crud() { | ||
Evento evento = new Evento(null, "Notebook", new Date()); | ||
|
||
// Criando a instância do DAO. | ||
EventoDAO dao = new EventoDAO(connection); | ||
|
||
// Fazendo a inserção e recuperando o identificador. | ||
Integer id = dao.salvar(evento); | ||
Assert.assertNotNull("Identificador foi retornado como NULL.", id); | ||
|
||
// Atribuindo o identificador retornado ao atributo "id". | ||
evento.setId(id); | ||
|
||
// Verificando se o registro realmente foi para o banco de dados. | ||
evento = dao.buscar(evento.getId()); | ||
Assert.assertNotNull("Evento nulo.", evento); | ||
|
||
// Atualizando o registro no banco de dados. | ||
String nomeAlterado = evento.getNome() + " alterado"; | ||
evento.setNome(nomeAlterado); | ||
dao.atualizar(evento); | ||
|
||
// Verificando se atualização ocorreu com sucesso. | ||
evento = dao.buscar(evento.getId()); | ||
Assert.assertEquals("O nome não foi atualizado corretamente.", nomeAlterado, evento.getNome()); | ||
|
||
// Removendo o registro. | ||
dao.deletar(evento.getId()); | ||
|
||
// O registro não existe mais. O método "buscar" deve retornar nulo. | ||
evento = dao.buscar(evento.getId()); | ||
Assert.assertNull("Evento ainda existe e não deveria.", evento); | ||
} | ||
} |
Oops, something went wrong.