将自动增量设置为先前值 JAVAPreparedStatement MYSQL

发布于 2024-10-11 19:42:13 字数 1403 浏览 3 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2024-10-18 19:42:13

使用单引号会导致mysql将auto_increment值(整数)转换为字符串,这是不可取的

剥离单引号,如

"ALTER TABLE employeemaster AUTO_INCREMENT=" + empno.getText()

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

"ALTER TABLE employeemaster AUTO_INCREMENT=" + empno.getText()

Or

cast empno.getText() into integer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文