Combobox + Arraylist + SQL

0

Hello, I have the following problem in question:

I made an array list to list data in a combo box, but it only lists the data in the first table, in the case: "catdesc", which is the description of a car category, whether it is Sports, etc. But in the same table, it has a column called catvalordiaria, that is, it is the daily value of this car category.

In the combobox only the description appears:

Iwanttoshow,nexttothisdescription,theamountofthedaily,ex:Sports-R$39.90;Idonotknowiftheproblemisbecausethevalueisdouble,orsla,Ineedyourhelp.Hereismy"CategoryDao" code and just below the Servicos.java, where it lists the categories in the Combo Box.

package model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import model.bean.Categoria;
import model.connection.ConnectionFactory;


public class CategoriaDao {

    Connection con=null;
    String sql;
    ResultSet rs;
    PreparedStatement pstm;

    ArrayList<Categoria> listacat=new ArrayList<>();

    public List<Categoria> listarCategorias(){

        try{           

         con=ConnectionFactory.getConnection();         
         sql= "SELECT catcodigo,catdesc,catvalordiaria FROM categoria ORDER BY catdesc";    
         pstm=con.prepareStatement(sql);            
         rs=pstm.executeQuery(sql);

         while(rs.next()){

         Categoria cat = new Categoria();

         cat.setCatCodigo(rs.getInt(1));
         cat.setCatDescricao(rs.getString(2));
         cat.setCatValorDiaria(rs.getDouble(3));
         listacat.add(cat);

         }

        }catch(Exception erro){

            JOptionPane.showMessageDialog(null,"Erro PSTM "+erro.getMessage());


        }

        return listacat;

            }

}
    public void preencherComboCategoria(JComboBox comboCategoria){

        CategoriaDao cat = new CategoriaDao();

        List<Categoria> listagem2 = cat.listarCategorias();

        for(Categoria c:listagem2){


            comboCategoria.addItem(c);


    }

}

}
    
asked by anonymous 19.04.2016 / 03:35

1 answer

0

It is possible to abstract the data needed for a new transition object, so you would not use the Categoria entity directly on a view layer, which is considered bad practice. That way you could have an attribute that is the concatenation of catdesc with catvalordiaria .

For example:

public class CategoriaViewModel {
    private int codigo;
    private String descricao;

    public String getCodigo() { return this.codigo; }
    public void setCodigo(String codigo) { this.codigo = codigo; }
    public String getDescricao() { return this.descricao; }
    public void setDescricao(String desc) { this.descricao = desc; } 
}

This would be your transition object, which you will display in the view. To get it we need to fill it in according to the data coming from the DAO layer. For this we can use a converter, where you will pass objects of type Categoria and it will convert to objects of type CategoriaViewModel .

public class CategoriaConversor() {

    /**
     * Em geral, os métodos de conversão não recebem tipos List,
     * e sim o tipo em si, no caso Categoria. 
     * Porém pra simplificar o exemplo será com list.
     **/
    public List<CategoriaViewModel> convert(List<Categoria> categorias) {
        List<CategoriaViewModel> viewModels = new ArrayList<CategoriaViewModel>();

        for (Categoria cat : categorias) { 
            CategoriaViewModel viewModel = new CategoriaViewModel();
            viewModel.setCodigo(cat.getCatCodigo());

            //Aqui é onde a descrição é concatenada e montada com desc + valordiaria
            viewModel.setDescricao(String.format("%s - %s", cat.getCatCodigo(),
                                   cat.getCatValorDiaria()));
            viewModels.add(viewModel);
        }

        return viewModels;
    }
}

And in your ComboBox fill method you convert to type CategoriaViewModel :

CategoriaConverter converter = new CategoriaConverter();
List<CategoriaViewModel> categorias = converter.convert(cat.listarCategorias());

for(CategoriaViewModel cat : categorias) {
    comboCategoria.add(cat);
}

This way you avoid using the direct entity object in the view layer, keeping it unchanged and true to what you brought it from the database if you use the same instance for other purposes.

    
19.04.2016 / 09:49