Auto Increment in number string type (0001 0002 ... java

0

Hello, I have a problem when generating a consecutive code. My data in the database is of type string , since it is not shown I do not know if the code will be well implemented.

DAO Class

public String Datosiguiente()
  {

    Connection acdb = cx.getConectar();
    String codigo="";
    Temporada t= new Temporada();
     String SQL="select max(TCodigo) from temporada";
    try {
        Statement st = acdb.createStatement();
        ResultSet rs=st.executeQuery(SQL);
        if(rs.next())
        {              
             codigo=rs.getString(1);
        }

    } catch (SQLException ex) {
       Logger.getLogger(MTemporadaDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    return codigo;

}

My Controller

 void numeros()

 {
    int j;
    int cont=1;
    String num="";
    String c="";
        if(c==null){
            t.getTxtcod().setText("0001");
        }
        else{
             j=Integer.parseInt(c);
             GenerarNumero gen= new GenerarNumero();
             gen.generar(j);
             t.getTxtcod().setText(gen.serie());

        }

}

Separate class

public class GenerarNumero {

private int dato;
private int cont=1;
private String num="";

public void generar(int dato) {
    this.dato = dato;
       if((this.dato>=1000) || (this.dato<10000)) 
       {
           int can=cont+this.dato;
           num = "" + can; 
       }
       if((this.dato>=100) || (this.dato<1000))
       {
           int can=cont+this.dato;
           num = "0" + can; 
       }
       if((this.dato>=9) || (this.dato<100)) 
       {
            int can=cont+this.dato;
           num = "00" + can; 
       }
       if (this.dato<9)
       {
           int can=cont+this.dato;
           num = "000" + can; 
       }

}

public String serie()
{
    return this.num;
}


}
    
asked by marwin yepez 13.06.2018 в 00:42
source

1 answer

0

You can place this method in your GenerateNumber class

public void generar(int dato) {
    this.num = String.format("%04d", dato);
}
    
answered by 13.06.2018 в 01:04