Results 1 to 5 of 5
Thread: Deleting from MySQL database
- 09-07-2012, 07:10 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
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();
}
}
- 09-07-2012, 08:21 AM #2
Re: Deleting from MySQL database
Why do they call it rush hour when nothing moves? - Robin Williams
- 09-07-2012, 08:34 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
- 09-07-2012, 10:18 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 09-07-2012, 10:19 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
deleting data from mysql database trough servlet / jsp
By Galichka in forum Advanced JavaReplies: 1Last Post: 05-24-2012, 12:43 AM -
Trouble using JSP for MySQL database
By killasaurus tex in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-13-2011, 06:39 AM -
Deleting rows from a database using JSP
By Manas Das in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 02-17-2009, 02:31 PM -
How to convert access database to mysql database?
By vrk in forum Advanced JavaReplies: 2Last Post: 02-11-2009, 04:43 AM -
Deleting records from database table using PreparedStatement
By Java Tip in forum Java TipReplies: 0Last Post: 02-09-2008, 08:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks