TarefaDAO.java
package br.com.tarefas.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.sql.Date;
import java.util.List;
import br.com.tarefas.conexao.Conexao;
import br.com.tarefas.modelo.Tarefa;
public class TarefaDAO {
// conexão com o banco de dados
private Connection connection;
// construtor da classe pegando a conexão
public TarefaDAO() {
this.connection = new Conexao().getMySQLConnection();
}
// Utilizando a conexão pelo Filtro
public TarefaDAO(Connection connection) {
this.connection = connection;
}
public void adiciona(Tarefa tarefa) {
String sql = "insert into tarefas (descricao) " + "values (?)";
try {
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setString(1, tarefa.getDescricao());
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public List<Tarefa> lista() {
try {
List<Tarefa> tarefas = new ArrayList<Tarefa>();
PreparedStatement stmt = this.connection.prepareStatement("select * from tarefas");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
// criando objeto tarefa
Tarefa tarefa = new Tarefa();
tarefa.setId(rs.getLong("id"));
tarefa.setDescricao(rs.getString("descricao"));
tarefa.setFinalizado(rs.getBoolean("finalizado"));
if (rs.getDate("dataFinalizacao") != null) {
// montando data atraves do calendar
Calendar dataFinalizacao = Calendar.getInstance();
dataFinalizacao.setTime(rs.getDate("dataFinalizacao"));
tarefa.setDataFinalizacao(dataFinalizacao);
}
// adicionar objeto a lista
tarefas.add(tarefa);
}
rs.close();
stmt.close();
System.out.println("Lista gerada com Sucesso!");
return tarefas;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void remove(Tarefa tarefa) {
try {
PreparedStatement stmt = this.connection.prepareStatement("delete from tarefas where id = ?");
stmt.setLong(1, tarefa.getId());
stmt.execute();
stmt.close();
System.out.println("Tarefa Excluida com SUCESSO!");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Tarefa buscaPorId(Long id) {
try {
PreparedStatement stmt = this.connection.prepareStatement("select * from tarefas");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
// System.out.println(rs.getLong("id")+ " // " + id);
if (id == rs.getLong("id")) {
// criando objeto tarefa
Tarefa tarefa = new Tarefa();
tarefa.setId(rs.getLong("id"));
tarefa.setDescricao(rs.getString("descricao"));
tarefa.setFinalizado(rs.getBoolean("finalizado"));
if (rs.getDate("dataFinalizacao") != null) {
// montando data atraves do calendar
Calendar dataFinalizacao = Calendar.getInstance();
dataFinalizacao.setTime(rs.getDate("dataFinalizacao"));
tarefa.setDataFinalizacao(dataFinalizacao);
}
System.out.println("retornada tarefa:" + tarefa.getId());
return tarefa;
}
}
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void altera(Tarefa tarefa) {
String sql = "update tarefas set descricao = ?, finalizado = ?, dataFinalizacao = ? where id = ?";
try {
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setString(1, tarefa.getDescricao());
stmt.setBoolean(2, tarefa.isFinalizado());
if (tarefa.getDataFinalizacao() != null) {
stmt.setDate(3, new Date(tarefa.getDataFinalizacao().getTimeInMillis()));
} else {
stmt.setDate(3, null);
}
stmt.setLong(4, tarefa.getId());
stmt.execute();
stmt.close();
System.out.println("DADOS ALTERADOS COM SUCESSO!");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void finaliza(Long id) {
Tarefa tarefa = new TarefaDAO().buscaPorId(id);
String sql = "update tarefas set finalizado = ?, dataFinalizacao = ? where id = ?";
try {
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setBoolean(1, true);
stmt.setDate(2, new Date(Calendar.getInstance().getTimeInMillis()));
stmt.setLong(3, tarefa.getId());
stmt.execute();
stmt.close();
System.out.println("DADOS ALTERADOS COM SUCESSO!");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Conexao.java
package br.com.tarefas.conexao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conexao {
public Connection getOracleConnection() {
System.out.println(“Conectando ao banco de dados Oracle…”);
Connection conn = null;
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
conn = DriverManager.getConnection(“jdbc:oracle:thin:@notebook:1521:xe”,“sepetiba”,“sepetiba”);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public Connection getMySQLConnection() {
System.out.println("Conectando ao banco de dados MySQL...");
try {
/*
* Esta linha de registro do driver salvou minha conexão!
* Ainda não sei o porque da obrigatoriedade dela!
* Ficar atento a isso da próxima vez!
*/
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
return DriverManager.getConnection("jdbc:mysql://localhost/banco_tarefa", "leonardo", "senha");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
AdicionarTarefasAction.java
package br.com.tarefas.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import br.com.tarefas.dao.TarefaDAO;
import br.com.tarefas.modelo.Tarefa;
public class AdicionarTarefasAction {
private Tarefa tarefa;
@Action(value = "adicionarTarefa", results = {
@Result(name = "ok", location = "tarefaAdicionada.jsp")
})
public String execute(){
new TarefaDAO().adiciona(tarefa);
return "ok";
}
public Tarefa getTarefa() {
return tarefa;
}
public void setTarefa(Tarefa tarefa) {
this.tarefa = tarefa;
}
}
e agora, mando dois prints de tela pra vc poder ver como está estruturada a aplicação no eclipse