Java Correlative Code

1

I would like to know how to generate a correlative code with this format "30200-01" the number 30200 is a code that is already established in the database, the number 01 is a number that each time you generate a document increases, " 30200-01 "" 30200-02 "" 30200-03 "and so on, but every time I log in I get this way:" 30200-1 "without the zero. This is the code that I am implementing: This method is to obtain the code "30200" already established in the database:

 void generarSerieDocumental(String centroCosto){
            Connection cn = null;
            PreparedStatement pstm = null;
            ResultSet rs = null;
            try {
                cn = new MySqlConexion().getConectar();
                String sql = "SELECT c_ccosto FROM FCCOSTO WHERE x_ccosto = ?";
                pstm = cn.prepareStatement(sql);
                pstm.setString(1, centroCosto);
                rs = pstm.executeQuery();
                while(rs.next()){
                    txtSerieDocumental.setText(rs.getString("c_ccosto"));
                }
            } 
            catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                try {
                    if (rs != null)
                        rs.close();
                    if (pstm != null)
                        pstm.close();
                    if (cn != null)
                        cn.close();
                } 
                catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }

This button add button, so that every time I click on the add button, the code is displayed in a table and the code "30200-01" "30200-02" is generated and so on when you click on the button

protected void btnAgregarActionPerformed(ActionEvent arg0) {
        DefaultTableModel model = (DefaultTableModel) tbSerieDocumental.getModel();
        String codigo = txtSerieDocumental.getText();
        int Item = tbSerieDocumental.getRowCount() + 1;
        String codigoSerial = codigo+"-"+Item;
        Object fila[]={codigoSerial,"","",""};
        model.addRow(fila);
    }
    
asked by Bruno 11.11.2016 в 17:37
source

2 answers

2

The problem is to believe that 01 is a number, quite the opposite is a text string.

What you are going to have to do is the following

int Item = tbSerieDocumental.getRowCount() + 1;
if(Item < 10){
   String codigoSerial = codigo+"-0"+Item;
}else{
   String codigoSerial = codigo+"-"+Item;
}

Add the if(Item < 10){ so that when the number is greater than 9, do not prepend the 0, if not the number that corresponds.

    
answered by 11.11.2016 / 17:41
source
3

There is a way to format that type of strings that comes in handy to make the padding of zeros you want and avoid the if/else . It would look like this:

String codigoSerial = String.format("%s-%02d", codigo, Item);

With that you indicate that you want the number to have 2 digits filling in zeros on the left if necessary.

    
answered by 11.11.2016 в 18:25