Return an SQL result

Asked

Viewed 454 times

-1

I have a query in sql that is returning me in a wrong way.

He’s returning like this:

[Cidade.Cidade@1d8b06a, Cidade.Cidade@251a5c, Cidade.Cidade@15ec3c1

and I’d like you to return like this

Acrelândia
Assis Brasil

Follow the code of the method:

City Class:

package Cidade;

public class Cidade {

    private String nom_cidade;
    private String estado;

    public String getNom_cidade() {
        return nom_cidade;
    }
    public void setNom_cidade(String nom_cidade) {
        this.nom_cidade = nom_cidade;
    }
    public String getEstado() {
        return estado;
    }
    public void setEstado(String estado) {
        this.estado = estado;
    }

    public String toString(){
            return this.nom_cidade;
    }
}

Classe Cidadedao

public class CidEstDao extends GenericDao {

    public List<Cidade> getCidadeByEstado(String nom_cidade) throws Exception {
        String select = "SELECT nom_cidade FROM cidade WHERE estado = ?";
        Cidade cidade = null;
        PreparedStatement stmt = getConnection().prepareStatement(select);
        List<Cidade> cidades = new ArrayList<Cidade>(); 
        stmt.setString(1, nom_cidade);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            cidade = new Cidade();
            cidade.setNom_cidade(rs.getString("nom_cidade"));
            cidades.add(cidade);
        }
        rs.close();
        stmt.close();
        return cidades;
    }
}

Class Cidadeestcontroller

public class CidEstController {

    public List<Cidade> buscaCidadePorEstado (String nom_cidade) throws Exception{
        CidEstDao dao = new CidEstDao();
        return  dao.getCidadeByEstado(nom_cidade);
    }
}

Main class

private List<Cidade> cidadePorEstado() throws Exception {
        CidEstController cc = new CidEstController();
        try {
            //List<Cidade> c = cc.buscaCidadePorEstado(uf);
             //List<Cidade> listaCidades = new ArrayList<>();
             //listaCidades = cc.buscaCidadePorEstado (uf);
           return cc.buscaCidadePorEstado (uf);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "Ocorreu um erro, tente novamente!n" + e.getLocalizedMessage());
        } catch (NullPointerException e){
            JOptionPane.showMessageDialog(this, "Contato não localizdo ou não existe!n" + e.getLocalizedMessage());
        }
        return Collections.emptyList();
    }
  • The issue only reinforces that this question is duplicated of any of the two linked, take a look at the two questions and their answers.

  • This can help you popular the Jcombobox

  • Sorry but the toString it compares two values, I do not want that and add items to combobox I already know. This query it searches the cities by state and to return all cities I put List<city> however when I add the values in a box or make a println gets those values and I want to know how to change the method so that out of it when adding to a box it goes out with the names of the cities.

  • 1

    The toString() does not compare anything, it just displays a representation created by the object’s java. The 3 links I posted solve your problem. The third I still explain how to display correctly on a combobox.

  • This can help you if you don’t want to overwrite toString(), but use a Remaster for the combo: Pick object id attribute selected in Jcombobox

Show 1 more comment

2 answers

3

What is explained in the links I posted of other questions is that to display something more personalized of an object instead of nomeDaClasse@hashcode, which is the default view created by java, you need to override the method toString() class, in your case, City.

Since you have given no information of this class, I will suggest a blind example:

@Override
public String toString(){
   return this.nome;//aqui você troca NOME pela 
                    //variavel que representa o nome da cidade na sua classe
}

Add this method to the class Cidade and problem solved.

  • I believe I did as you said but I could not, I’m working with sql query and catching up. I changed the post and put the classes if you can help me, I really appreciate lost.

  • @Did you add the method in the City class as I suggested? If so, the problem is solved.

  • I even added it to the title to see, but it didn’t work.

  • @Diegogomesdias then the problem is not in the codes you presented in the question. Then you need to present a "[mcve]" of your code in order to be able to test it.

  • I put only the method of each class referring to the city query, city class, classeDao, classeController and the main class. How do I make the return not hashcode, you can analyze what I did, please.

  • @There’s no other solution to suggest without you presenting a testable code. See if you don’t happen to be importing city class from another package or another project. Other than that, there’s no way to help you with past information.

  • @Diegogomesdias Did the answer help you? If yes, you can mark it as accepted by clicking on v the left of the answer :)

Show 2 more comments

-2


I managed to solve my problem as follows

try {
            for (Cidade a : cidadePorEstado()) 
                boxCidade.addItem(String.valueOf(a));

                    ;
        } catch (Exception ex) {
            Logger.getLogger(CadCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
  • This does not solve the problem of the question. Your doubt has nothing to do with the proposed answer.

  • Actually you, yes, I kept the main class as it was, and to display instead of using the toString that returned me like this: [City.City@1d8b06a, City@251a5c, City.City@15ec3c1 I used this method Try, it returns the names of the cities CORRECTLY within the BOX according to the state I choose. If you want I can send the project to you in full and even Voce can analyze. Thanks for everything.

  • There are only two ways to solve this, which is by rewriting the tostring, as I showed you, and how you added in your city class (question code) or by making a combomodel. Remove the tostring method from your city class and try to use this code from your answer to see. It won’t work, it’s the tostring that changes the display, as I showed in the answer.

Browser other questions tagged

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