Results 1 to 3 of 3
  1. #1
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Deleting a MySQL database row in Java

    I have a problem that maybe my eyes are just over looking. I have a method that I created to delete a specific row in a MySQL database. I am using a Connect object I also created to connect to the database. In the method, I am trying to delete the row where the integer column studentID equals the integer returned from getstudentID() and the varchar column firstName equals the String returned from getfirstName(). When I run the application and call this method below it runs successfully. Well it acts that way, but the row is not deleted from the database. With this code below, I don't receive any errors at all. If anyone could help me out here I would appreciate it.

    Java Code:
    public void delete() {
            Statement stmt = null;
    
            try
            {
                Connect conn = new Connect();
                stmt=conn.makeStatement();
    
    
                //Create SQL statement to delete
    
                stmt.execute("Delete FROM student WHERE studentID=" + "'" + getstudentID() + "'"
                         + " AND 'firstName'=" + "'" + getfirstName() + "'");
                                                                 
                stmt.close();
                conn.close();
            }
             catch (SQLException e) 
             {
                e.printStackTrace();
                System.err.println("Could not delete student");
    
             } 
            System.out.println("Successfully deleted student from the database");
        }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: Deleting a MySQL database row in Java

    Quote Originally Posted by dalythe View Post
    Java Code:
                stmt.execute("Delete FROM student WHERE studentID=" + "'" + getstudentID() + "'"
                         + " AND 'firstName'=" + "'" + getfirstName() + "'");
    Why are you quoting the column name firstName? Tip: use a PreparedStatement instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: Deleting a MySQL database row in Java

    It was just that. I did not need to quote the column name. Thanks a bunch.

Similar Threads

  1. Deleting from MySQL database
    By darrenstyles88 in forum New To Java
    Replies: 4
    Last Post: 09-07-2012, 11:19 AM
  2. deleting data from mysql database trough servlet / jsp
    By Galichka in forum Advanced Java
    Replies: 1
    Last Post: 05-24-2012, 01:43 AM
  3. Replies: 5
    Last Post: 09-16-2011, 07:58 AM
  4. Replies: 2
    Last Post: 06-12-2008, 08:09 AM
  5. MySQL Database and Java
    By shaggymac in forum Advanced Java
    Replies: 1
    Last Post: 05-01-2008, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •