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);
}
}
}