从Java wamp服务器中删除数据库

时间:2022-10-23 18:03:47
public void deleteBook(int temp)
{
    try
    {
        query = "DELETE FROM ccItems WHERE ISBN = '"+temp+"'";

        stmt.executeUpdate(query);

        JOptionPane.showMessageDialog(null, "Item Deleted !");
    }      

    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, "Error: Could not find ISBN or Already deleted.");
    }
}

I have a deleteBook() method where it would delete items in my database. It works properly meaning deleting the specified item with the corresponding ISBN entered. Problem is, if the user inputs an ISBN which is not in my database the try statement still executes. I think the catch statement must execute because the entered ISBN is not found in database.

我有一个deleteBook()方法,它将删除我的数据库中的项目。它正常工作意味着删除输入相应ISBN的指定项目。问题是,如果用户输入的ISBN不在我的数据库中,则try语句仍然会执行。我认为必须执行catch语句,因为在数据库中找不到输入的ISBN。

5 个解决方案

#1


0  

As in a select query, 'No records found' is not an exception.stmt.executeUpdate(query); will return the number of records deleted.

与在select查询中一样,“找不到记录”不是例外.stmt.executeUpdate(查询);将返回已删除的记录数。

#2


0  

if i input something to make temp equals ' or ''=',all your ccItems will be deleted,that is a SQL injection attack.

如果我输入的东西使得temp等于'或''=',那么你的所有ccItem都将被删除,这就是SQL注入攻击。

#3


0  

It won't give any error because if the ISBN does not exist, it just effects zero rows without any error.

它不会给出任何错误,因为如果ISBN不存在,它只会产生零行而没有任何错误。

If you want to know exactly if any row is deleted, write the code like this.

如果您想确切知道是否删除了任何行,请编写如下代码。

public void deleteBook(int temp){
   try{
        query = "DELETE FROM ccItems WHERE ISBN = '"+temp+"'";
        int numberOfRowsEffected = stmt.executeUpdate(query);
        if (numberOfRowsEffected == 0){
            JOptionPane.showMessageDialog(null, "Item not found !");
        }else{
            JOptionPane.showMessageDialog(null, "Item Deleted !");
        }
    } catch(Exception e){
        JOptionPane.showMessageDialog(null, "Error: Could not find ISBN or Already deleted.");
    }
}

#4


0  

The try will still run. It just deletes 0 rows.

尝试仍然会运行。它只删除0行。

You need to use the return value of executeUpdate(). Since executeUpdate() returns an int; the number of entities updated or deleted you can use it to solve your problem. You can just throw an exception to get your error message.

您需要使用executeUpdate()的返回值。由于executeUpdate()返回一个int;更新或删除的实体数量,您可以使用它来解决您的问题。您可以抛出异常来获取错误消息。

public void deleteBook(int temp)
{
    try
    {
        query = "DELETE FROM ccItems WHERE ISBN = '"+temp+"'";
        if (stmt.executeUpdate(query)!=0) {
          JOptionPane.showMessageDialog(null, "Item Deleted !");
        } else {
          throw new Exception();
        }
    }      
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, "Error: Could not find ISBN or Already deleted.");
    }
}

#5


0  

From database point of view that is not an error at all, it just didn't find any matching records given your where condition. That's why you don't have exception thrown on client.

从数据库的角度来看,根本不是一个错误,它只是找不到任何匹配的记录给出你的where条件。这就是为什么你没有在客户端抛出异常的原因。

#1


0  

As in a select query, 'No records found' is not an exception.stmt.executeUpdate(query); will return the number of records deleted.

与在select查询中一样,“找不到记录”不是例外.stmt.executeUpdate(查询);将返回已删除的记录数。

#2


0  

if i input something to make temp equals ' or ''=',all your ccItems will be deleted,that is a SQL injection attack.

如果我输入的东西使得temp等于'或''=',那么你的所有ccItem都将被删除,这就是SQL注入攻击。

#3


0  

It won't give any error because if the ISBN does not exist, it just effects zero rows without any error.

它不会给出任何错误,因为如果ISBN不存在,它只会产生零行而没有任何错误。

If you want to know exactly if any row is deleted, write the code like this.

如果您想确切知道是否删除了任何行,请编写如下代码。

public void deleteBook(int temp){
   try{
        query = "DELETE FROM ccItems WHERE ISBN = '"+temp+"'";
        int numberOfRowsEffected = stmt.executeUpdate(query);
        if (numberOfRowsEffected == 0){
            JOptionPane.showMessageDialog(null, "Item not found !");
        }else{
            JOptionPane.showMessageDialog(null, "Item Deleted !");
        }
    } catch(Exception e){
        JOptionPane.showMessageDialog(null, "Error: Could not find ISBN or Already deleted.");
    }
}

#4


0  

The try will still run. It just deletes 0 rows.

尝试仍然会运行。它只删除0行。

You need to use the return value of executeUpdate(). Since executeUpdate() returns an int; the number of entities updated or deleted you can use it to solve your problem. You can just throw an exception to get your error message.

您需要使用executeUpdate()的返回值。由于executeUpdate()返回一个int;更新或删除的实体数量,您可以使用它来解决您的问题。您可以抛出异常来获取错误消息。

public void deleteBook(int temp)
{
    try
    {
        query = "DELETE FROM ccItems WHERE ISBN = '"+temp+"'";
        if (stmt.executeUpdate(query)!=0) {
          JOptionPane.showMessageDialog(null, "Item Deleted !");
        } else {
          throw new Exception();
        }
    }      
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, "Error: Could not find ISBN or Already deleted.");
    }
}

#5


0  

From database point of view that is not an error at all, it just didn't find any matching records given your where condition. That's why you don't have exception thrown on client.

从数据库的角度来看,根本不是一个错误,它只是找不到任何匹配的记录给出你的where条件。这就是为什么你没有在客户端抛出异常的原因。