Sql命令删除从文本字段中获取的值

时间:2013-05-15 17:11:10

标签: java sql tomcat jdbc

我使用此命令插入一个新客户端,我在Java中有一个方法,我将值分配给命令:

insertNewPerson = connection.prepareStatement(
                    "INSERT INTO Addresses " + 
                    "(FirstName, LastName, Email, PhoneNumber, ClientAddress ) " +
                    "VALUES (?, ?, ?, ?, ?)" );

问题:我正在寻找类似的东西,但是要删除程序文本字段中的任何条目。

这是我用来设置sql命令中的值的Java方法:

public int addPerson(
            String fname, String lname, String email, String num, String caddress)
    {
        int result = 0;

        //set parameters, then execute insertNewPerson
        try {
            insertNewPerson.setString(1, fname);
            insertNewPerson.setString(2, lname);
            insertNewPerson.setString(3, email);
            insertNewPerson.setString(4, num);
            insertNewPerson.setString(5, caddress);

            //insert the new entry; return # of rows updated
            result = insertNewPerson.executeUpdate();
        }//end try
        catch(SQLException sqlException) {
            sqlException.printStackTrace();
            close();
        }//end catch

        return result;
    }//end method addPerson

2 个答案:

答案 0 :(得分:1)

据我了解,您正在寻找DELETE SQL命令:

delete from Addresses 
where FirstName = ?
and LastName = ?
and Email = ?
and PhoneNumber = ?
and ClientAddress = ?

这也考虑到你需要根据字段上的值删除。

答案 1 :(得分:1)

您应该拥有所有记录的唯一值。如果您有主键,则可以使用此块。

deletePerson = connection.prepareStatement(
    "DELETE FROM Addresses " + 
    "WHERE personID = ?" );