Typing error when receiving foreign key from the database

Asked

Viewed 27 times

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

  • 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

  • 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.

  • Besides what @Maniero said, your line 1 casos.setCidades(rs.getstring(("cidade_nomecidade")); is with the getstring (with lowercase string’S').

1 answer

0

There is no type Cidade and Doenca in the database, and yes in Java code. What the error is saying is that you are picking up a string, which is the result of rs.getString("cidade_nomecidade") and is trying to put her in an object City with the caso.setCidades().

"city_nameserve" and "doenca_nameserve" are the foreign keys to the city and disease tables.

So for example, you have 3 tables, with the following columns:

  • cases: idCaso(int), qtdCaso(int), city_nameserve(String), does_nameserve(String)
  • city: city name(String), state(String)
  • disease: disease name(String), priority(int)

To fill the Java object Caso you need to make an sql query that takes the data from the three tables, using a Join:

select * from casos
inner join cidade on cidade_nomecidade = nomecidade
inner join doenca on doenca_nomedoenca = nomedoenca

And then create the case object, and the city and disease objects from the returned data:

Caso caso = new Caso();
caso.setIdCaso(rs.getInt("idCaso"));
caso.setQtdCaso(rs.getInt("qtdCaso"));

Cidade cidade = new Cidade();
cidade.setNome(rs.getString(("nomecidade"));
cidade.setEstado(rs.getString(("estado"));
caso.setCidade(cidade);

Doenca doenca = new Doenca();
doenca.setNome(rs.getString(("nomedoenca"));
doenca.setPrioridade(rs.getInt(("prioridade"));
caso.setDoenca(doenca);

casos.add(caso);

Note: If possible, change these class, attribute, variable, and table names to the singular. Only List<Caso> casos should be in the plural. It is also preferable to use numbers for foreign identifiers and keys, and objects (e.g. Integer and Long instead of int and long) in Java. For example:

  • cases: idCaso (Long), qtdCaso (Long), city_id (Long), doenca_id (Long)
  • city: id(Long), name(String)
  • disease: id(Long), disease name(String), priority(Integer)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.