将自动增量设置为先前值 JAVAPreparedStatement MYSQL
我有 3 个按钮(添加、保存、取消)。如果我按下添加按钮,它会自动生成一个自动递增的值并显示在文本字段中。如果我按下保存按钮,它就会更新记录。我的问题是,当我按下取消按钮时,我希望能够删除当前添加的数据并将自增键设置为已删除数据的主键。可以这样做吗?
dc.connect();
try {
PreparedStatement st=dc.getConnection().prepareStatement("Delete from Employeemaster where empno = '" + empno.getText() + "'");
i=st.executeUpdate();
if (i>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>0) {
dc.getConnection().commit();
}
} catch (Exception e) {
JOptionPane msg=new JOptionPane();
msg.showMessageDialog(this,"Database Error: "+e.getMessage());
}
我尝试替换
ALTER TABLE employeemaster AUTO_INCREMENT ='" + empno.getText() + "'"
成
ALTER TABLE employeemaster AUTO_INCREMENT = 10003;
并且它起作用了。是否可以将自动递增值设置为文本字段中包含/输入的值?
附加信息: 我得到的错误是
“您的 SQL 语法有错误;请检查与您的 MYSQL 服务器版本对应的手册,以获取在第 1 行“10003”附近使用的正确语法。”
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?
dc.connect();
try {
PreparedStatement st=dc.getConnection().prepareStatement("Delete from Employeemaster where empno = '" + empno.getText() + "'");
i=st.executeUpdate();
if (i>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>0) {
dc.getConnection().commit();
}
} catch (Exception e) {
JOptionPane msg=new JOptionPane();
msg.showMessageDialog(this,"Database Error: "+e.getMessage());
}
I tried replacing
ALTER TABLE employeemaster AUTO_INCREMENT ='" + empno.getText() + "'"
into
ALTER TABLE employeemaster AUTO_INCREMENT = 10003;
and it worked. Is it possible to set the auto-incremented value to the one contained/entered in a textfield?
Additional info:
The error I get is
"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."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用单引号会导致mysql将auto_increment值(整数)转换为字符串,这是不可取的
剥离单引号,如
或
将
empno.getText()
转换为整数The use of single quote will cause mysql cast the auto_increment value (integer) into string, which is not desirable
Strip the single quote, like
Or
cast
empno.getText()
into integer