vendredi 6 mai 2016

Avoid double method for class

I have this two method:

public ArrayList<IvaModel> Tabella(String query){
    ArrayList<IvaModel> elenco = new ArrayList<IvaModel>();
    try{
        con=Connessione.ConnessioneDB();
        stmt = con.createStatement();
        rs =  stmt.executeQuery(query);
        while (rs.next()) {
            IvaModel c = new IvaModel();
            c.setCodice(rs.getString(1));
            c.setDescrizione(rs.getString(2));
            c.setAliquota(rs.getInt(3));
            c.setDetraibilità(rs.getInt(4));
            c.setTipoOperione(rs.getString(5));
            elenco.add(this);
        }
     }
    catch(Exception ex){

    }
    finally{
         try{
            rs.close();
            stmt.close();
            con.close();
        }
        catch (SQLException ex) {
            System.out.println(ex);
        }
    }
    return elenco;
}

public void Visualizza(String query){
    try{
        con=Connessione.ConnessioneDB();
        stmt = con.createStatement();
        rs =  stmt.executeQuery(query);
        while (rs.next()) {
            setCodice(rs.getString(1));
            setDescrizione(rs.getString(2));
            setAliquota(rs.getInt(3));
            setDetraibilità(rs.getInt(4));
            setTipoOperione(rs.getString(5));
                        }
     }
   catch(Exception ex){

    }
   finally{
        try{
           rs.close();
           stmt.close();
            con.close();
          } 
         catch (SQLException ex) {
                System.out.println(ex);
        }
    }

}

Visualizza is for search single record of table sqllite and table is for list and add every single record in arrayLst. I can use only method for two toll. Can i do all in a single method??

Aucun commentaire:

Enregistrer un commentaire