@Bianca_Souza escreveu:
Estou começando a estudar java e tentando desenvolver uma aplicação CRUD com mysql. Segue abaixo um exemplo:
Funcionarios.java
public class Funcionarios {
private int id;
private Long rg;
private Long cpf;
private String nome;
private Filiais filial;public Filiais getFilial() { return filial; } public void setFilial(Filiais filial) { this.filial = filial; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Long getRg() { return rg; } public void setRg(Long rg) { this.rg = rg; } public Long getCpf() { return cpf; } public void setCpf(Long cpf) { this.cpf = cpf; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; }
}
FuncionariosDAO.java
public class FuncionariosDAO {
public void create(Funcionarios f){ Connection c = Conexao.getConnection(); PreparedStatement stmt = null; try{ stmt = c.prepareStatement("INSERT INTO funcionarios (rg,cpf,nome,id_filial) VALUES (?,?,?,?)", Statement.RETURN_GENERATED_KEYS); stmt.setLong(1, f.getRg()); stmt.setLong(2, f.getCpf()); stmt.setString(3, f.getNome()); stmt.setInt(4, f.getFilial().getId()); stmt.executeUpdate(); ResultSet rs = stmt.getGeneratedKeys(); rs.next(); int idGerado = rs.getInt(1); f.setId(idGerado); JOptionPane.showMessageDialog(null, "Funcionário cadastrado com sucesso!"); } catch(SQLException ex){ Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE,null,ex); JOptionPane.showMessageDialog(null, "Erro ao cadastrar!"); } finally{ Conexao.Desconectar(c, stmt); } } public List<Funcionarios> read(){ Connection c = Conexao.getConnection(); PreparedStatement stmt = null; ResultSet rs = null; List<Funcionarios> funcs = new ArrayList<>(); try{ stmt = c.prepareStatement("SELECT * FROM funcionarios"); rs = stmt.executeQuery(); while(rs.next()){ Funcionarios f = new Funcionarios(); Filiais f1 = new Filiais(); f.setId(rs.getInt("id")); f.setRg(rs.getLong("rg")); f.setCpf(rs.getLong("cpf")); f.setNome(rs.getString("nome")); f.setFilial((Filiais) rs.getObject(f1.getId())); /*ERRO*/ funcs.add(f); } } catch(SQLException ex){ Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE,null,ex); } finally{ Conexao.Desconectar(c, stmt, rs); } return funcs; } public void update(Funcionarios f){ Connection c = Conexao.getConnection(); PreparedStatement stmt = null; try{ stmt = c.prepareStatement("UPDATE funcionarios SET rg = ?,cpf = ?, nome = ?, id_filial = ?where id = ?"); stmt.setLong(1, f.getRg()); stmt.setLong(2, f.getCpf()); stmt.setString(3, f.getNome()); stmt.setInt(4, f.getFilial().getId()); stmt.setInt(7, f.getId()); stmt.executeUpdate(); JOptionPane.showMessageDialog(null, "Atualizado com sucesso!"); } catch(SQLException ex){ Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE,null,ex); JOptionPane.showMessageDialog(null, "Erro ao atualizar registro!"); } finally{ Conexao.Desconectar(c, stmt); } } public void delete(Funcionarios f){ Connection c = Conexao.getConnection(); PreparedStatement stmt = null; try{ stmt = c.prepareStatement("DELETE FROM funcionarios where id = ?"); stmt.setInt(1, f.getId()); stmt.executeUpdate(); JOptionPane.showMessageDialog(null, "Registro excluido!"); } catch(SQLException ex){ Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE,null,ex); JOptionPane.showMessageDialog(null, "Erro ao excluir registro!"); } finally{ Conexao.Desconectar(c, stmt); } }
}
O meu problema está no método read() que irá listar todo o conteúdo da tabela. Tenho um id_filial (Tabela Filiais) como chave estrangeira e não sei como fazer referência a este campo através do getObject.
Mensagens: 2
Participantes: 2