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);
}