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();
}
}
Re: Deleting from MySQL database
Re: Deleting from MySQL database
Quote:
Originally Posted by
DarrylBurke
What is wrong with the db then darryl?!Thanks
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.
Re: Deleting from MySQL database
Quote:
Originally Posted by
Tolls
That's a very wrong.
And I appear to have become Chico Marx.