Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Deleting from MySQL database

    Hi,
    I have a hr recruitment application that lists job vacancies.I want to be able to delete a job from the database when an action button is clicked.
    I am using a mysql database with struts and cannot delete specific records!! It has me annoyed all night!!:(
    Here is the action i am calling while passing in the ID of the job I want to delete:
    It is running through the action but does not delete the job from the database:(

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class RemoveJob {
    private int IDjob;
    private String result ="SUCCESS";
    private Connection connection;
    private Statement statement;

    public RemoveJob() {

    }

    private void loadDriver() throws ClassNotFoundException{
    Class.forName("com.mysql.jdbc.Driver");
    }

    // Setup the connection with the DB
    private void makeConnection() throws SQLException {
    connection=DriverManager.getConnection("jdbc:mysql ://localhost/portal?user=user&password=password");
    }

    public void buildStatement() throws SQLException {
    statement = connection.createStatement();
    }

    //execute update


    public String execute() throws Exception {
    try {
    this.loadDriver();
    this.makeConnection();
    String sql = "DELETE * FROM portal.job where job.IDjob ='"+IDjob+"'";
    int delete = statement.executeUpdate(sql);
    System.out.println("in try");

    }
    finally{
    this.closeConnections();
    return result;
    }
    }

    /**
    * Closes prepared statements and the connection to the database.
    */
    public void closeConnections() {
    try {
    connection.close();
    }
    catch (SQLException e) {
    System.out.println("");
    e.printStackTrace();
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: Deleting from MySQL database

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Deleting from MySQL database

    What is wrong with the db then darryl?!Thanks

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Deleting from MySQL database

    Darryl is pointing out that you clearly haven't read the forum rules.
    One of which is to post code using [code] tags [/code] since people generally won't read unformatted code.

    I will say you need more debug code in there.
    You don't (for example) print the value of the id you're trying to delete.
    ALso you have a 'return' statement in a finally block. That's a very wrong.
    That should be after the finally block.
    I'll explain that if you want.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: Deleting from MySQL database

    Quote Originally Posted by Tolls View Post
    That's a very wrong.
    And I appear to have become Chico Marx.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. deleting data from mysql database trough servlet / jsp
    By Galichka in forum Advanced Java
    Replies: 1
    Last Post: 05-24-2012, 12:43 AM
  2. Trouble using JSP for MySQL database
    By killasaurus tex in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-13-2011, 06:39 AM
  3. Deleting rows from a database using JSP
    By Manas Das in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 02-17-2009, 02:31 PM
  4. Replies: 2
    Last Post: 02-11-2009, 04:43 AM
  5. Replies: 0
    Last Post: 02-09-2008, 08:31 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
  •