JComboBox does not load with data

2

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

    
asked by reymagnus 01.10.2018 в 15:53
source

2 answers

0

I answer my question with the answer they gave me in Stackoverflow in English:

  

You are adding the same instance again and again. Create a new one   Instance in each iteration (removing the previous declaration   also):

       while (rs.next())
       {
          AbastosOpLog abasop = new AbastosOpLog(); 
          abasop.setAbas_cod(rs.getString("abascod"));
          abasop.setAbas_desc(rs.getString("abasdesc"));
          abastosop.add(abasop);
       } 
    
answered by 03.10.2018 / 15:14
source
0

The error seems to be in Query's query ...

private void cargarDivisiones() {
    conectar cc = new conectar();
    Connection cn = cc.conexion();


        String nombrecliente = (String) cmbCliente.getSelectedItem();
        System.out.println(nombrecliente);

        String query = "SELECT * FROM clientes WHERE nombrecliente LIKE nombrecliente";
        try {
            PreparedStatement pst = cn.prepareStatement(query);
            result = pst.executeQuery();
            while (result.next()) {
                cmbDivision.addItem(result.getString("divisiones"));  //aca es donde cargas la informacion de la query en el combobox 
            }

            cn.close();
        } catch (SQLException ex) {
            Logger.getLogger(Loq_Insertar.class.getName()).log(Level.SEVERE, null, ex);
        }


}
    
answered by 01.10.2018 в 16:40