Reputation: 11
Firstly, you can check the image in the link. I want to update and delete a table row by clicking "pilih aksi". anyone can help me? thank you.
Upvotes: -1
Views: 99
Reputation: 23
Update Process:
For this process, we first right click on our table and add the Mouse Click event>Mouse clicked event.
Inside this event part:
lbl_id.setText(jTable1.getValueAt(jTable1.getSelectedRow(),0).toString());
txt_name.setText(jTable1.getValueAt(jTable1.getSelectedRow(),1).toString());
txt_surname.setText(jTable1.getValueAt(jTable1.getSelectedRow(),2).toString());
txt_email.setText(jTable1.getValueAt(jTable1.getSelectedRow(),3).toString());
txt_password.setText(jTable1.getValueAt(jTable1.getSelectedRow(),4).toString());
To summarize, first of all, we sent the data from the table to the label and textfield fields. Now we leave the values in the label and textfield fields and place them in the question mark places in the sql update query. We use PreparedStatement while doing this.
Delete Operation:
String id=lbl_id. getText ( ) ;
String query= "DELETE FROM `users` WHERE id=?" ;
PreparedStatement ps;
try {
ps=MyConnection. getConnection ( ) . prepareStatement ( query ) ;
ps. setString ( 1 , id ) ;
ps. executeUpdate ( ) ;
if ( ps. executeUpdate ( ) == 0 ) {
JOptionPane. showMessageDialog ( null, "Deleted" ) ;
}
get_value ( ) ;
} catch ( Exception e ) {
}
We do the same with the delete query. This time all we need is the id. So we just got the id value. After the process is finished, we called the function to pull data from the table again. Thus, our table will be updated after deletion or update.
Upvotes: 1