Buscar

EventosJdbc Copia (1)

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

package br.unip.sicc.eventos.dao;
import br.unip.sicc.uf.model.Uf;
import br.unip.sicc.uf.model.TipoUf;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
public class UfJdbc implements UfDao {
 
 private static UfJdbc instance;
 
 static {
 instance = new UfJdbc();
 }
 public static UfJdbc getInstance() {
 return instance;
 }
 
 private UfJdbc(){}
 private static final String SQL_UPDATE
 = "UPDATE TB_UF "
 + " SET TIPO = ? , " //1 indice do parametro
 + " TIPO = ? , " //2
 + " NOMEOFICIAL = ? , " //3
 + " POPULACAO = ? , " //4
 + " WHERE ID = ?"; //5
 @Override
 public void atualizar(Uf uf) throws UfDaoException {
 Connection conn = null;
 PreparedStatement stmt = null;
 try {
 // recuperar a conexao
 conn = GerenciadorUf.getUf();
 // utlizar a uf para recuperar o comando
 stmt = conn.prepareStatement(SQL_UPDATE);
 // parametrizar o comando
 stmt.setString(1, uf.getTipo().toString());
 // parametrizar o comando
 stmt.setString(2, uf.getTitulo());
 // parametrizar o comando
 stmt.setString(3, uf.getLocal());
 //convertendo java.util.Date para java.sql.Date
 long dataEmMiliSegundos = uf.getDataInicio().getTime();
 Timestamp dateTimeSql = new Timestamp(dataEmMiliSegundos);
 // parametrizar o comando
 stmt.setTimestamp(4, dateTimeSql);
 //convertendo java.util.Date para java.sql.Date
 dataEmMiliSegundos = uf.getDataTermino().getTime();
 dateTimeSql = new Timestamp(dataEmMiliSegundos);
 stmt.setTimestamp(5, dateTimeSql);
 
 System.out.println("Registros atualizados: " + qtdeRegistros);
 } catch (SQLException ex) {
 ex.printStackTrace();
 throw new UfDaoException(
 "Não foi possível atualizar", ex);
 } finally {
 GerenciadorUf.fechar(conn, stmt);
 }
 }
 private static final String SQL_INSERT
 = "INSERT INTO TB_Uf "
 + "(TIPO, NOMEOFICIAL, POPULACAO) "
 + " VALUES(?, ?, ?, ?, ?)";
// 1 2 3 4 5 
 @Override
 public void incluir(Uf uf) throws UfDaoException {
 Connection conn = null;
 PreparedStatement stmt = null;
 try {
 conn = GerenciadorUf.getUf();
 stmt = conn.prepareStatement(SQL_INSERT);
 stmt.setString(1, uf.getTipo().name());
 stmt.setString(2, uf.getNomeOficial());
 stmt.setString(3, uf.getPopulacao());
 stmt.setTimestamp(4, dateTimeSql);
 
 stmt.setTimestamp(5, dateTimeSql);
 stmt.setInt(6, uf.getId());
 int qtdeId = stmt.executeUpdate
 
 GerenciadorUf.fechar(conn, stmt);
 }
 }
 private static final String SQL_SELECT_TODOS
 = "SELECT ID, NOMEOFICIAL"
 + " POPULACAO FROM TB_UF";
 @Override
 public List<Uf> getTodos() throws UfDaoException {
 List<Uf> listaDeUf = new ArrayList<>();
 Connection conn = null;
 PreparedStatement stmt = null;
 ResultSet rs = null;
 try {
 conn = GerenciadorUf.getConexaoUf();
 stmt = conn.prepareStatement(SQL_SELECT_TODOS);
 rs = stmt.executeQuery();
 while(rs.next()){
 int id = rs.getInt("ID");
 String tipoStr = rs.getString("TIPO");
 // convertendo String para TipoUf
 TipoUf tipo = TipoUf.valueOf(tipoStr);
 String titulo = rs.getString("NOMEOFICIAL");
 String local = rs.getString("POPULACAO");
 
 int populacao = rs.getInt("POPULACAO");
 Uf uf = new Evento(id, tipo, nomeOficial, populacao);
 listaUf.add(uf);
 }
 } catch (SQLException ex) {
 throw new UfDaoException("Não foi possível incluir o uf", ex);
 } finally {
 GerenciadorUf.fechar(conn, stmt, rs);
 }
 return listaUf;
 }
 private static final String SQL_SELECT_POR_TIPO
 = "SELECT ID, TIPO, NOMEOFICIAL"
 + " POPULACAO "
 + " FROM TB_UF "
 + " WHERE TIPO = ?";
 @Override
 public List<Uf> getUfPorTipo(TipoUf tipoSelecionado) throws UfDaoException {
 List<Uf> listaUf = new ArrayList<>();
 Connection conn = null;
 PreparedStatement stmt = null;
 ResultSet rs = null;
 try {
 conn = GerenciadorUf.getUf();
 stmt = conn.prepareStatement(SQL_SELECT_POR_TIPO);
 stmt.setString(1, tipoSelecionado.toString());
 rs = stmt.executeQuery();
 while(rs.next()){
 int id = rs.getInt("ID");
 String tipoStr = rs.getString("TIPO");
 // convertendo String para TipoId
 TipoEvento tipo = TipoId.valueOf(tipoStr);
 String titulo = rs.getString("NOMEOFICIAL");
 String local = rs.getString("POPULACAO");
 
 Uf uf = new Uf(id, tipo, nomeOficial, populacao);
 listaUf.add(uf);
 }
 } catch (SQLException ex) {
 throw new UfDaoException("Não foi possível incluir o uf", ex);
 } finally {
 GerenciadorUf.fechar(conn, stmt, rs);
 }
 return listaUf;
 }
 private static final String SQL_DELETE = 
 "DELETE FROM TB_UF WHERE ID=";
 @Override
 public void excluir(Uf uf) throws UfDaoException {
 Connection conn = null;
 Statement stmt = null;
 try {
 conn = GerenciadorUf.getUf();
 stmt = conn.createStatement();
 int id = Uf.getId();
 
UfDaoException("Não foi possível excluir o uf", ex);
 } finally {
 GerenciadorUf.fechar(conn, stmt);
 }
 }
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais