Deleting record from database HELP! :(
Hi everyone, I am new here. I hope I am in the right place about my problem.
Okay here it is, I have this Employee Information System, and I already have things almost done, but my problem is when hitting the delete button, it automatically deletes the record without confirmation.
http://img33.imageshack.us/i/javaapp.jpg/
By the way, this is the code for my delete button. Please help me what I should do to give this a confirmation yes or no prompt before deleting. Your help will be appreciated. Thanks!
Code Java:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
datacon.GetConnection();
String q = "DELETE from info WHERE ( IDno = '" + jComboBox1.getSelectedItem() +"')";
try
{
st = datacon.GetConnection().createStatement();
st.executeUpdate(q);
st.close();
datacon.GetConnection().close();
JOptionPane.showMessageDialog(null, " Successfully Deleted!");
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage());
}
firstn.setText(null);
middlen.setText(null);
lastn.setText(null);
eadd.setText(null);
hadd.setText(null);
city.setText(null);
zip.setText(null);
country.setText(null);
hphone.setText(null);
workphone.setText(null);
sss.setText(null);
hiredate.setText(null);
salary.setText(null);
dept.setText(null);
supervisor.setText(null);
econtact.setText(null);
ephone.setText(null);[QUOTE][QUOTE][/QUOTE][/QUOTE]
Re: Deleting record from database HELP! :(
See How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) It explains how to easily create a confirmation dialog. You can create the dialog, retrieve the user input (as in integer), and use this only if the value has been confirmed.
A simple example you can work from
Re: Deleting record from database HELP! :(
Hey, thank you so much for the response, I'll give it a go! :D
Re: Deleting record from database HELP! :(
Hi again, I have read the article that you suggested, and I came up to this problem. I have successfully made a prompt for delete confirmation that lets you choose YES and NO, but the problem is that, even if I chose the option NO, it still deletes the record, it's like same function as the first option (YES button).
So uhmn I just want to clarify if something is wrong with my codes, I'm just new to java and this is one of our assignments. I'm really having hard time figuring this out. I hope you can help me with this one too. Thanks so much!
Code Java:
datacon.GetConnection();
JOptionPane.showConfirmDialog(null, "Delete Record?", "Delete Confirmation", JOptionPane.YES_NO_OPTION);
int n = 0;
if ( n == JOptionPane.YES_OPTION ){
String q = "DELETE from info WHERE ( IDno = '" + jComboBox1.getSelectedItem() +"')";
try
{
st = datacon.GetConnection().createStatement();
st.executeUpdate(q);
st.close();
datacon.GetConnection().close();
JOptionPane.showMessageDialog(null, " Successfully Deleted!");
}
catch(SQLException ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage());
}
firstn.setText(null);
middlen.setText(null);
lastn.setText(null);
eadd.setText(null);
hadd.setText(null);
city.setText(null);
zip.setText(null);
country.setText(null);
hphone.setText(null);
workphone.setText(null);
sss.setText(null);
hiredate.setText(null);
salary.setText(null);
dept.setText(null);
supervisor.setText(null);
econtact.setText(null);
ephone.setText(null);
}
else if ( n == JOptionPane.NO_OPTION ){
firstn.setText("");
middlen.setText("");
lastn.setText("");
eadd.setText("");
hadd.setText("");
city.setText("");
zip.setText("");
country.setText("");
hphone.setText("");
workphone.setText("");
sss.setText("");
hiredate.setText("");
salary.setText("");
dept.setText("");
supervisor.setText("");
econtact.setText("");
ephone.setText("");
}
}