如何删除记录(字符串)JAVA和MYSQL

前端之家收集整理的这篇文章主要介绍了如何删除记录(字符串)JAVA和MYSQL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我成功地删除了一个整数,但是当我试图将它变为STRING时它会说
“where子句中的未知列itemtodelete但是我的ITEMTODELETE是在数据库中声明的STRING不是一个整数它不删除一个STRING多少?

下面是我的代码

private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        int del = (prompt):
        if (del == JOptionPane.YES_OPTION){
        DelCurRec();
        }

    }     


   public void DelCurRec() {

        String id = field.getText();
        String sql = "DELETE FROM inventory WHERE ItemCode = "+id+" ";

        try {
           Class.forName(connectio);
       }  catch (Exception e) {
           JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC Driver Error",JOptionPane.WARNING_MESSAGE);
       }

        Statement stmt = null;
        Connection con = null;

        //Creates connection to database
        try {
            con = DriverManager.getConnection("Connection");
            stmt = con.createStatement();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"Connection Error",JOptionPane.WARNING_MESSAGE);
        }

        //Execute the sql statment for deleting records
        try {
            stmt.executeUpdate(sql);
            //This closes the connection to the database
            con.close();
            //This closes the dialog
            JOptionPane.showMessageDialog(null,"Deleted Succesfully","Delete Successful",JOptionPane.WARNING_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"Communication Error",JOptionPane.WARNING_MESSAGE);
        }
    }

解决方法

尝试更改线路:
String sql = "DELETE FROM inventory WHERE ItemCode = "+id+" ";

String sql = "DELETE FROM inventory WHERE ItemCode = '"+id+"' ";
原文链接:https://www.f2er.com/java/122938.html

猜你在找的Java相关文章