Cordial greeting. I am trying to develop a desktop application in Java and when loading the data from a JComboBox they do not appear. I am using a layered model. This is the DAO code:
public static ArrayList<AbastosOpLog> LisAbasDisp(){
ArrayList <AbastosOpLog> abastosop = new ArrayList<>();
Connection connection = null;
Conexion conex = new Conexion();
PreparedStatement statement = null;
ResultSet result = null;
AbastosOpLog abasop = new AbastosOpLog();
connection = conex.getConnection();
String consulta = "select abascod , abasdesc from abop001";
try{
if(connection != null){
statement = connection.prepareStatement(consulta);
result = statement.executeQuery();
while(result.next() == true)
{
abasop.setAbas_cod(result.getString("abas_cod"));
abasop.setAbas_desc(result.getString("abas_desc"));
abastosop.add(abasop);
}
}
}catch (SQLException e) {
System.out.println("Error en la consulta de Abastos: "+e.getMessage());
}finally{
try {
connection.close();
conex.desconectar();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return abastosop;
}
And the method that loads it in the view:
private void LlenarOpAbastos() {
cblisopt.removeAllItems();
AbastosOpDao miAbastosOpDao = new AbastosOpDao();
ArrayList <AbastosOpLog> abastoop = AbastosOpDao.LisAbasDisp();
for (int i = 0; i < abastoop.size(); i++) {
cblisopt.addItem(abastoop.get(i).getAbas_cod() + "-" + abastoop.get(i).getAbas_desc());
}
//lblMsj.setText(cblisopt.getSelectedItem().toString());
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
Where can the error be ?, the code I designed from this one that I found. link thanks for your help.
/ ********************************************* ****************************** /
I am already loaded with JComboBox
. Yes there was an error in the query SQL
.
But now I load the last Item
of the list and it brings me 15 times the last value of the list.
The ResultSet
reads the last row 15 times.
This is the try
that sets the values:
try{
if(connection != null){
statement = connection.prepareStatement(consulta);
result = statement.executeQuery();
while(result.next() == true)
{
abasop.setAbas_cod(result.getString("abas_cod"));
abasop.setAbas_desc(result.getString("abas_desc"));
abastosop.add(abasop);
}
}
}
And the code that fills the JComboBox
private void LlenarOpAbastos() {
cblisopt.removeAllItems();
AbastosOpDao miAbastosOpDao = new AbastosOpDao();
ArrayList <AbastosOpLog> abastoop = AbastosOpDao.LisAbasDisp();
for (int i = 0; i < abastoop.size(); i++) {
cblisopt.addItem(abastoop.get(i).getAbas_cod() + "-" + abastoop.get(i).getAbas_desc());
}
//lblMsj.setText(cblisopt.getSelectedItem().toString());
}