Create invoice number in java

2

I want to create a method that generates an invoice number.

I do not increase it in the DB because it is not recommended and I have decided to create a char varying field where I store or add 1 to the query in postgres.

public Cliente numOrden() {

                PreparedStatement ps = null;
                ResultSet rs = null;
                Connection con = getConexionTemporal();
                // Preparamos la consulta
                String sql ="select cli_num from cliente where cli_num=cli_num";
                try {
                    // Traemos los datos de la bd
                    ps = con.prepareStatement(sql);
                    rs = ps.executeQuery();
                    // Cargamos los resultados
                    if (rs.next()) {
                        Cliente cliente = new Cliente();
                        cliente.setNumeroOrden(rs.getString("cli_num"));
                        return cliente;
                    }

                } catch (SQLException e) {
                    e.printStackTrace();

                } finally {

                    cerrarObjeto(con);
                    cerrarObjeto(rs);
                    cerrarObjeto(ps);

                }
                return null;

            }

The controller where I show the invoice number:

private void cargarOrden(){

        SentenciasSQL registro = new SentenciasSQL();
        Cliente cliente = registro.numOrden();

        txtFactura.setText(cliente.getNumeroOrden());
        System.out.println(cliente.getNumeroOrden());
    }

If you help me with any idea or how it would be easy, I want it to be 00001 and follow 00002 etc.

    
asked by Nicolas Mosquera Espinosa 21.03.2018 в 16:15
source

3 answers

1

You can make a count to the table customers assuming that you have 2 records would return an integer 2 and so we could add the value that follows +1

Query:

 SELECT COUNT(cli_num) as cli_num FROM clientes

Then in your method instead of returning a client-type object you return an integer int

            public int numOrden() {

                PreparedStatement ps = null;
                ResultSet rs = null;
                Connection con = getConexionTemporal();
                // Preparamos la consulta
                String sql ="SELECT COUNT(cli_num) as cli_num FROM clientes";
                try {
                    // Traemos los datos de la bd
                    ps = con.prepareStatement(sql);
                    rs = ps.executeQuery();
                    // Cargamos los resultados
                    if (rs.next()) {
                        int cliente = rs.getInt("cli_num");
                        return cliente;
                    }

                } catch (SQLException e) {
                    e.printStackTrace();

                } finally {

                    cerrarObjeto(con);
                    cerrarObjeto(rs);
                    cerrarObjeto(ps);

                }
                return null;

            }

And your method to obtain the invoice number would be as follows:

    private void cargarOrden(){
        //Numero de ceros para rellenar el consecutivo de la factura
        int NUMERO_CEROS = 5;

        SentenciasSQL registro = new SentenciasSQL();
        int cliente = registro.numOrden() + 1;

        numeroConsecutivo = rellenarConCeros(String.valueOf(cliente), NUMERO_CEROS);


        txtFactura.setText(numeroConsecutivo);
        System.out.println(numeroConsecutivo);
    }


    private String rellenarConCeros(String cadena, int numCeros) {
        String ceros = "";

        for (int i = cadena.length(); i < numCeros; i++) {
            ceros += "0";
        }

        return ceros + cadena;
    }
    
answered by 21.03.2018 / 16:41
source
0

What you could do is convert the number of String to Integer to add one and then do a conversion of the Integer to the format you want to save in BD , I leave the following code:

//Se recibe como parámetro el número en forma de String que se trae de la consulta.
private String convertirNumero(String numero){
     //Se hace el formato del String.
     DecimalFormat format = new DecimalFormat("00000");
     //Se realiza la convesión del String recibido como parámetro y se le suma 1.
     return format.format(Integer.valueOf(numero) + 1);
}

Testing the method:

String número = "001";
System.out.println("Conversión: "+convertirNumero(numero));
Salida: Conversión: 00002

Adapting it to your code would be as follows:

try {
    // Traemos los datos de la bd
    ps = con.prepareStatement(sql);
    rs = ps.executeQuery();
    // Cargamos los resultados
    if (rs.next()) {
        Cliente cliente = new Cliente();
        cliente.setNumeroOrden(this.convertirNumero(rs.getString("cli_num")));
        return cliente;
    }

} catch (SQLException e) {
    e.printStackTrace();
}
    
answered by 21.03.2018 в 16:43
0

I am learning to program in netBeans and I would like to know if someone could help me. I want that when I select a receipt I generate a series and invoice number, that is, where it says SerieNum (001) and don says NumeroCom (000001) and so on, but when the NumeroCom reaches 9999999 the SerieNum becomes 002. Can someone help me?

    
answered by 26.10.2018 в 19:50