javamysqlconnector-j

Java, mySQL, connector-j, deleting a contact


I'm trying to delete a contact from an address book that uses a MySQL database and I thought this would be how it was done, however an exception is thrown and it prints "Unknown column 'john' in 'where clause'". Each contact has 5 attributes - name, mobile, homephone, email and address and one of the contacts name is 'john'.

public static void deleteContact(String name)
{
     Connection con = connect();// function that returns mySQL function
     Statement s = con.createStatement();
     try{
         s.executeUpdate("Delete from contactDetails where name =" + name);
     }
     catch(SQLException e){
         System.out.println(e.getMessage());
     }
}

Solution

  • Surround your variable value with quotes.

    "... where name ='" + name + "'");