How I can use an auto increment in java for a register system?

152 Views Asked by At

I'm creating a register system for students using files, while I use the program the auto increment works fine, but when I ruin it again, the autoincremnt returns 1, also I am not allowed to use MySQL DataBase I initialize it in the principal form

public class FrmPrincipal extends javax.swing.JFrame {
  AdmonOrgSecuencial a = new AdmonOrgSecuencial("Datos.dat","Id.dat");
  public static int idProveedor=1;
  
    /**
     * Creates new form FrmPrincipal
     */
    public FrmPrincipal() {
        initComponents();
        Idp gen = new Idp(idProveedor);
        a.idEscribir(gen);
    }

This is the code I use to create and display the students

public class FrmAltas extends javax.swing.JDialog {
  AdmonOrgSecuencial a = new AdmonOrgSecuencial("Datos.dat", "Id.dat");
    
    /**
     * Creates new form FrmAltas
     */
    
    public FrmAltas(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
       Idp proid;
        int cve;
       int clave;
       int generador=FrmPrincipal.idProveedor;
               proid= a.busquedaId(generador);
               clave = proid.getIdp();
        txtClave.setText(String.valueOf(clave));
    }

  private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {                                          
      Idp proid;
      
      int clave;
      int generador= FrmPrincipal.idProveedor;
      proid=  a.busquedaId(generador);
    
        clave = proid.getIdp();
              

        String nombre = txtNombre.getText();
        float promedio = Float.parseFloat(txtPromedio.getText());
        Alumno alu = new Alumno(clave, nombre, promedio);
        a.altas(alu);
         
         a.bajasId(generador);
         FrmPrincipal.idProveedor=generador+1;
         int mas=FrmPrincipal.idProveedor;
         Idp proid2= new Idp(mas);
         
        a.idEscribir(proid2);

        
        this.dispose();
    }  

These are the methods I use

 public void idEscribir(Idp cve){
        try{
           {
            fos = new FileOutputStream(idArchivo, true);
            dos = new DataOutputStream(fos);
            dos.writeInt(cve.getIdp());
            cont++;
            }
        }
        catch(FileNotFoundException e){
            
        }
        catch(IOException e){
            System.out.println("Error:" +e.getMessage());
        }finally{
            cerrarArchivoEscritura();
        }
        
       }
         
public void bajasId(int elmr){
        copiarIncremental(elmr);
        borrarRenombrarId();
    }
    public void copiarIncremental(int idc){
       int idg;
       try{
           fis = new FileInputStream(idArchivo);
           dis = new DataInputStream(fis);
           fos = new FileOutputStream("Temporario.dat");
           dos = new DataOutputStream(fos);
           while(true){
               idg = dis.readInt();
               
               if(idg != idc){
                   dos.writeInt(idg);
                   
               }
           }
       }catch(EOFException e){
           
       }catch(IOException e){
           System.out.println("Error"+ e.getMessage());
       }finally{
           cerrarArchivoLectura();
           cerrarArchivoEscritura();
       }
   }
    
  public void borrarRenombrarId(){
      File original = new File(idArchivo);
      original.delete();
      File temporal = new File("Temporario.dat");
      temporal.renameTo(original);
  }
public Idp busquedaId(int clave){
         boolean existe= false;
        int cve;
        
           try{
               fis= new FileInputStream(idArchivo);
               dis= new DataInputStream(fis);
               while(existe == false){
                   cve = dis.readInt();
                   
                if(clave == cve){           
                    existe = true;
                    return new Idp(clave);
                   }
              
               }
                return null;
           }catch(FileNotFoundException e){
               return null;
           }
           catch(EOFException e){
               return null;
           }catch(IOException e){
               System.out.println("Error"+ e.getMessage());
               return null;
           }finally{
           cerrarArchivoLectura();
       }  
    
    }
1

There are 1 best solutions below

2
ubw On BEST ANSWER

Programs store their data in main memory. On program exit, all this data is lost. Therefore, when you restart your program, all the fields (like your auto-increment number) will no longer hold their former values.

To fix this, you need to write the auto-increment number to disk, e.g. by storing it in a file. Then, on program start, you want to load the previously saved number back into main memory.