Note that there are some explanatory texts on larger screens.

plurals
  1. POSet Auto-Increment into previous value JAVA PreparedStatement MYSQL
    text
    copied!<p>I have 3 buttons (add, save, cancel). If I press the add button, it automatically generates an auto-incremented value and is displayed in a text field. If I press the save button, it updates the record. My problem is when I press the cancel button, I want to be able to delete the current data added and set the auto-increment key to the primary key of the deleted data. Is it possible to do this?</p> <pre><code>dc.connect(); try { PreparedStatement st=dc.getConnection().prepareStatement("Delete from Employeemaster where empno = '" + empno.getText() + "'"); i=st.executeUpdate(); if (i&gt;0) { dc.getConnection().commit(); } } catch (Exception e) { JOptionPane msg=new JOptionPane(); msg.showMessageDialog(this,"Database Error: "+e.getMessage()); } dc.disconnect(); dc.connect(); try { PreparedStatement st=dc.getConnection().prepareStatement("ALTER TABLE employeemaster AUTO_INCREMENT ='" + empno.getText() + "'"); i=st.executeUpdate(); if (i&gt;0) { dc.getConnection().commit(); } } catch (Exception e) { JOptionPane msg=new JOptionPane(); msg.showMessageDialog(this,"Database Error: "+e.getMessage()); } </code></pre> <p>I tried replacing </p> <pre><code>ALTER TABLE employeemaster AUTO_INCREMENT ='" + empno.getText() + "'" </code></pre> <p>into </p> <pre><code>ALTER TABLE employeemaster AUTO_INCREMENT = 10003; </code></pre> <p>and it worked. Is it possible to set the auto-incremented value to the one contained/entered in a textfield?</p> <p>Additional info: The error I get is </p> <blockquote> <p>"You have an error in your SQL syntax; check the manual that corresponds to your MYSQL server version for the right syntax to use near "10003" at line 1."</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload