簡體   English   中英

將自動增量設置為以前的值JAVA PreparedStatement MYSQL

[英]Set Auto-Increment into previous value JAVA PreparedStatement 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”附近使用的正確語法。”

使用單引號將導致mysql將auto_increment值(整數)轉換為字符串,這是不希望的

去除單引號,例如

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

要么

empno.getText()轉換為整數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM