I have created a table named student including column stu_id, stu_name & Blood_group.I created a delete button to delete rows and I used the following code when delete button is pressed.I take input using jtextfield.But this can only delete by taking input in stu_id.But now i also want to delete by Blood_group or stu_name or both.here is my code.
private void cmd_deleteActionPerformed(java.awt.event.ActionEvent evt) {
int p= JOptionPane.showConfirmDialog(null,"Do you really want to delete?","Delete",JOptionPane.YES_NO_OPTION);
if(p==0){
try{
String sql="Delete from student where stu_id=?";
pst=conn.prepareStatement(sql);
pst.setString(1, jTextField1.getText());
pst.execute();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Update_table();
}
}
I figured i have to change my query.So i changed my code to this.
private void cmd_deleteActionPerformed(java.awt.event.ActionEvent evt) {
int p= JOptionPane.showConfirmDialog(null,"Do you really want to delete?","Delete",JOptionPane.YES_NO_OPTION);
if(p==0){
try{
String s[]=new String[20];
s[1]=jTextField1.getText();
s[2]=jTextField2.getText();
s[3]=jTextField3.getText();
String sql="Delete from student where ";
if(s[1] != null)
sql=sql+"stu_id='"+s[1]+"' AND ";
if(s[2] != null)
sql=sql+"stu_name='"+s[2]+"' AND ";
if(s[3]!= null)
sql=sql+"Blood_group='"+s[3]+"'";
pst=conn.prepareStatement(sql);
pst.execute();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Update_table();
}
}
But this didn't work.Unfortunately now i have to give input all 3 column to delete a row.How can i solve that?Did my ques was clear.Thanx in advance.