title: Can I get the id of the last inserted value which is int with prefix from mysql to Java?
body: <p>is there a way how to get the last inserted id that has a prefix for example <code>SIN0000001</code>. In the table <code>sales</code> the primary key was working well but when my code in java pass the <code>sales</code> id to table <code>product_sales</code> its only value was <code>0</code>.</p>
  <p>I created three tables which are the following:</p>
  <p><code>sales_seq</code> as the trigger <a href="https://i.stack.imgur.com/SoLjb.png" rel="nofollow noreferrer">table1</a></p>
  <p><code>sales</code> where it enters the date, amount due, cash, and balance <a href="https://i.stack.imgur.com/SVXCI.png" rel="nofollow noreferrer">table2</a></p>
  <p><code>product_sales</code> where it should include the <code>sales</code> primary key/id, product barcode, price, qty, and total <a href="https://i.stack.imgur.com/5HsMX.png" rel="nofollow noreferrer">table3</a></p>
  <p>This is my code from the class <code>Sell</code>:</p>
  <pre><code>public class Sell extends javax.swing.JFrame {
  
  /**
   * Creates new form Category
   */
  public Sell() {
      initComponents();
      Connect();
  
      //table_update();
  }
  
  Connection con;
  String databaseName = &quot;&quot;;
  String url = &quot;jdbc:mysql://localhost:3306/&quot; + databaseName;
  PreparedStatement pst;
  ResultSet rs;
  DefaultTableModel table_model = new DefaultTableModel();
  
  
  String username = &quot;root&quot;;
  String password = &quot;odeja007&quot;;
  
  public void Connect(){
      try{
          Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);
          con = DriverManager.getConnection(url, username, password);
      }
      catch(ClassNotFoundException ex){
      }
      catch(SQLException ex){
      }
  }
  
  private void table_pos() {
      // TODO add your handling code here:
      String itemCode = jTextField_itemBarcode.getText();
  
      try {
          pst = con.prepareStatement(&quot;SELECT * FROM dnk_database.add_items WHERE item_barcode = ?&quot;);
          pst.setString(1, itemCode);
          ResultSet rs = pst.executeQuery();
          
          while(rs.next()){
              int current_qty;
  
              current_qty = rs.getInt(&quot;quantity&quot;);
  
              int price = Integer.parseInt(jTextField_price.getText());
              int load = Integer.parseInt(jTextField_quantity.getText());
  
              int total_amount = price * load;
              
              if(load &gt;= current_qty){
                  JOptionPane.showMessageDialog(this, &quot;Quantity exceeds the stocks limit!&quot;);
              }else{
                  table_model = (DefaultTableModel)jTable_itemList.getModel();
                  table_model.addRow(new Object[]{
                      jTextField_itemBarcode.getText(),
                      jTextField_itemName.getText(),
                      jTextField_price.getText(),
                      jTextField_quantity.getText(),
                      total_amount,
                  });
                  
                  int sum = 0;
                  
                  for(int i = 0; i &lt; jTable_itemList.getRowCount(); i++){
                      sum = sum + Integer.parseInt(jTable_itemList.getValueAt(i, 4).toString());
                      
                  }
                  
                  jTextField_amountDue.setText(Integer.toString(sum));
                  
                  jTextField_itemBarcode.setText(&quot;&quot;);
                  jTextField_itemName.setText(&quot;&quot;);
                  jTextField_price.setText(&quot;&quot;);
                  jTextField_availability.setText(&quot;&quot;);
                  jTextField_stocks.setText(&quot;&quot;);
                  jTextArea_description.setText(&quot;&quot;);
                  jTextField_quantity.setText(&quot;&quot;);
                  jTextField_amount.setText(&quot;&quot;);
                  jTextField_itemBarcode.requestFocus();
              }
          }
      } catch (SQLException ex) {
          Logger.getLogger(Sell.class.getName()).log(Level.SEVERE, null, ex);
      }
          
      
  }
  
  private void jButton_addActionPerformed(java.awt.event.ActionEvent evt) {                                            
      // TODO add your handling code here:
      table_pos();
  }                                           
  
  private void jButton_processActionPerformed(java.awt.event.ActionEvent evt) {                                                
      // TODO add your handling code here:
      //int total_amount = Integer.parseInt(jTextField_amountDue.getText());
      Payment payment = new Payment();
      payment.jTextField_amountDuePayment.setText(jTextField_amountDue.getText());
      payment.setVisible(true);
  }                                               
  
  private void jButton_voidActionPerformed(java.awt.event.ActionEvent evt) {                                             
      // TODO add your handling code here:
      table_model.removeRow(jTable_itemList.getSelectedRow());
      
      int sum = 0;
                  
      for(int i = 0; i &lt; jTable_itemList.getRowCount(); i++){
          sum = sum + Integer.parseInt(jTable_itemList.getValueAt(i, 4).toString());
  
      }
  
      jTextField_amountDue.setText(Integer.toString(sum));
  }                                            
  
  private void jTextField_quantityKeyReleased(java.awt.event.KeyEvent evt) {                                                
      // TODO add your handling code here:
      int price = Integer.parseInt(jTextField_price.getText());
      int load = Integer.parseInt(jTextField_quantity.getText());
  
      int total_amount = price * load;
      String amount = String.valueOf(total_amount);
      
      if(jTextField_quantity.getText().equals(&quot;&quot;)){
          jTextField_amount.setText(&quot;&quot;);
      }else{
          jTextField_amount.setText(amount);
      }
      
  }                                               
  
  private void jTextField_itemBarcodeKeyPressed(java.awt.event.KeyEvent evt) {                                                  
      // TODO add your handling code here:
      if(evt.getKeyCode() == KeyEvent.VK_ENTER){
          try {
              String itemCode = jTextField_itemBarcode.getText();
  
              pst = con.prepareStatement(&quot;SELECT * FROM dnk_database.add_items WHERE item_barcode = ?&quot;);
              pst.setString(1, itemCode);
              ResultSet rs = pst.executeQuery();
  
              if(rs.next()==false)
              {
                  JOptionPane.showMessageDialog(this, &quot;Item not found!&quot;);
              }else{
                  String itemName = rs.getString(2);
                  String Price = rs.getString(5);
                  String Availabilty = rs.getString(7);
                  String Stocks = rs.getString(4);
                  String Description = rs.getString(3);
  
                  jTextField_itemName.setText(itemName);
                  jTextField_price.setText(Price);
                  jTextField_availability.setText(Availabilty);
                  jTextField_stocks.setText(Stocks);
                  jTextArea_description.setText(Description);
  
              }   
          }catch (SQLException ex) {
          
          }  
      }
  }
  </code></pre>
  <p>and the class for <code>Payment</code>:</p>
  <pre><code>public class Payment extends javax.swing.JFrame {
  
  /**
   * Creates new form Payment
   */
  public Payment() {
      initComponents();
      Connect();
  }
  
  
  Connection con;
  String databaseName = &quot;&quot;;
  String url = &quot;jdbc:mysql://localhost:3306/&quot; + databaseName;
  PreparedStatement pst;
  ResultSet rs;
  DefaultTableModel table_model = new DefaultTableModel();
  
  
  String username = &quot;root&quot;;
  String password = &quot;odeja007&quot;;
  
  public void Connect(){
      try{
          Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);
          con = DriverManager.getConnection(url, username, password);
      }
      catch(ClassNotFoundException ex){
      }
      catch(SQLException ex){
      }
  }
  
  private void jButton_exitActionPerformed(java.awt.event.ActionEvent evt) {                                             
      // TODO add your handling code here:
      dispose();
  }                                            
  
  private void sales(){
      // TODO add your handling code here:
      DateTimeFormatter dtf = DateTimeFormatter.ofPattern(&quot;MM/dd/yyyy&quot;);
      LocalDateTime now = LocalDateTime.now();
      
      String date = dtf.format(now);
      String amount_due = jTextField_amountDuePayment.getText();
      String cash = jTextField_cash.getText();
      String balance = jTextField_balance.getText();
      int last_id = 0;
      
      try {
          String sale_query = &quot;INSERT INTO dnk_database.sales (`date`, `amount_due`, `cash`,  `balance`) VALUES (?,?,?,?);&quot;;
          pst = con.prepareStatement(sale_query, Statement.RETURN_GENERATED_KEYS);
          pst.setString(1, date);
          pst.setString(2, amount_due);
          pst.setString(3, cash);
          pst.setString(4, balance);
          pst.executeUpdate();
          ResultSet rs = pst.getGeneratedKeys();
          
          if(rs.next()){
              last_id = rs.getInt(1);
          }
          
          JOptionPane.showMessageDialog(this, last_id);
          
          //Sell sell = new Sell();
          int rows = jTable_itemList.getRowCount();
          
          String product_query = &quot;INSERT INTO `dnk_database`.`product_sales` (`sales_number`, `product_barcode`, `sell_price`,  `quantity`,  `total`) VALUES (?,?,?,?,?);&quot;;
          pst = con.prepareStatement(product_query);
          
          String product_barcode = &quot;&quot;;
          String price = &quot;&quot;;
          String qty = &quot;&quot;;
          int total = 0;
          
          for(int i = 0; i &lt; jTable_itemList.getRowCount(); i++){
              product_barcode = (String)jTable_itemList.getValueAt(i, 0);
              price = (String)jTable_itemList.getValueAt(i, 2);
              qty = (String)jTable_itemList.getValueAt(i, 3);
              total = (int)jTable_itemList.getValueAt(i, 4);
              
              pst.setInt(1, last_id);
              pst.setString(2, product_barcode);
              pst.setString(3, price);
              pst.setString(4, qty);
              pst.setInt(5, total);
              pst.executeUpdate();
          }
          
          pst.addBatch();
          JOptionPane.showMessageDialog(this, &quot;Succesfully recorded!&quot;);
          
      } catch (SQLException ex) {
          Logger.getLogger(Payment.class.getName()).log(Level.SEVERE, null, ex);
      }
      
      
  }
  
  
  private void jButton_printActionPerformed(java.awt.event.ActionEvent evt) {                                              
      // TODO add your handling code here:
      int cash = Integer.parseInt(jTextField_cash.getText());
      int total = Integer.parseInt(jTextField_amountDuePayment.getText());
      int change = cash - total;
      
      jTextField_balance.setText(String.valueOf(change));
      
      sales();
  }                                             
  
  private void jButton_finishActionPerformed(java.awt.event.ActionEvent evt) {                                               
      // TODO add your handling code here:
  }                                              
  
  private void jButton_cancelActionPerformed(java.awt.event.ActionEvent evt) {                                               
      // TODO add your handling code here:
  }
  </code></pre>