0
Good evening, basically this is a list I did to show the data that were entered in the database, but I have been finding typing problems on lines 1 and 2 (marked as comment), as predicted, the getstring function would only work with strings, however I need to receive "cidade_nomecidade" and "doenca_nomedoenca" (from the column in the database) which are of the type City and Disease. Is there any way I can get some get on these foreign keys within that context ? The error generated is: "incompatible types:String cannot be converted to Cities"
public List<Casos> read(){
Connection con = ConnectionFactory.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
List<Casos> caso = new ArrayList<>();
try {
stmt = con.prepareStatement("SELECT * FROM casos");
rs = stmt.executeQuery();
while(rs.next()){
Casos casos = new Casos();
casos.setIdcaso(rs.getInt("idcaso"));
casos.setQtdCaso(rs.getInt("qtdCaso"));
casos.setCidades(rs.getstring(("cidade_nomecidade")); //LINHA 1
casos.setDoenca(rs.getString("doenca_nomedoenca")); //LINHA 2
caso.add(casos);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Falha na leitura");
}finally{
ConnectionFactory.closeConnection(con, stmt, rs);
}
return caso;
}
public class Casos {
private int idcaso;
private int qtdCaso;
private Doenca doenca;
private Cidades cidades;
public Doenca getDoenca() {
return doenca;
}
public void setDoenca(Doenca doenca) {
this.doenca = doenca;
}
public Cidades getCidades() {
return cidades;
}
public void setCidades(Cidades cidades) {
this.cidades = cidades;
}
public int getQtdCaso() {
return qtdCaso;
}
public void setQtdCaso(int qtdCaso) {
this.qtdCaso = qtdCaso;
}
public int getIdcaso() {
return idcaso;
}
public void setIdcaso(int idcaso) {
this.idcaso = idcaso;
}
}
Look.. if the type of content in the database is text... you should use the
getString()
and adjust your setters to build the object you expect– Leandro Angelo
You must take the key of the City objects and call the DAO to load it too, or if you only need the Instantiative ID and set the Id
– nullptr
Mysql does not allow creating types, so these columns cannot be the type you are talking about. The first thing you need to do is define correctly what you are doing.
– Maniero
Besides what @Maniero said, your line 1
casos.setCidades(rs.getstring(("cidade_nomecidade"));
is with thegetstring
(with lowercase string’S').– Elyel Rubens da Rosa