3
I’m having a little trouble implementing the foreign keys in my example. Can someone help me assemble the DAO class?
Cidadedao.java
public class CidadeDAO {
private final Connection connection;
public CidadeDAO() {
try {
this.connection = new ConnectionFactory().getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void adiciona(Cidade cidade){
String sql= "insert into cidade (nome, cep, id_estado) values (?,?,?)";
PreparedStatement stmt;
try{
stmt= connection.prepareStatement(sql);
stmt.setString(1, cidade.getNome());
stmt.setString(2, cidade.getCep());
// como faço o setString do estado?
stmt.execute();
stmt.close();
}catch(SQLException e){
throw new RuntimeException(e);
}
}
}
Estadodao.java.
public class EstadoDAO {
private final Connection connection;
public EstadoDAO() {
try {
this.connection = new ConnectionFactory().getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void adiciona(Estado estado){
String sql= "insert into estado (nome, sigla, regiao) values (?,?,?)";
PreparedStatement stmt;
try{
stmt= connection.prepareStatement(sql);
stmt.setString(1, estado.getNome());
stmt.setString(2, estado.getSigla());
stmt.setString(3, estado.getRegiao());
stmt.execute();
stmt.close();
}catch(SQLException e){
throw new RuntimeException(e);
}
}
}
Java city.
public class Cidade {
private Long id;
private String nome;
private String cep;
private Estado estado;
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 getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public Estado getEstado() {
return estado;
}
public void setEstado(Estado estado) {
this.estado = estado;
}
}
Java state.
public class Estado {
private Long id;
private String nome;
private String sigla;
private String regiao;
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 getSigla() {
return sigla;
}
public void setSigla(String sigla) {
this.sigla = sigla;
}
public String getRegiao() {
return regiao;
}
public void setRegiao(String regiao) {
this.regiao = regiao;
}
}
Which exception/error occurs even?
– Cold
I haven’t implemented the city test class yet. First I need to know how to set the foreign keys to be able to give the Insert. Could you give me an example?
– simplexception
In the method
adiciona()
in the City class, the city passed as argument must already have the state set in it, so just take the state id by doingcidade.getEstado().getId()
. That’s if you’re storing the id in the comic book, if that’s the namecidade.getEstado().getNome()
, but I recommend it to be the id, because it should be set as unique in the BD, so it is not to occur inconsistencies.– Math
And in the main class, what should I put? It would be something like "city.setState(existing);"?
– simplexception
Excuse the indiscretion of the question, but is your objective in this case didactic (learn)? Or do you want to create an application of own use or commercial? If it is the second option, have you thought about using JPA or even a framework for development? Because many of these basic questions would already be solved and you could go straight to "business".
– Saito
I’m really trying to learn. See how it works, you know? I’m going to leave the frameworks for later.
– simplexception