Name: Date Performed: Rating
Name: Date Performed: Rating
Jalandoni
Name:
Date Performed:
Rating:
Course/Section:
Date Submitted:
ACTIVITY
JAVA AND MYSQL DATABASE
INTENDED LEARNING OUTCOME
Create a Java application that is integrated with a MySQL database.
BACKGROUND
DELETING DATA
Add another button named btn_delete and change the text into Delete.
Since before we can delete we need to connect, we will just call the dbtester
this.btn_dbTesterActionPerformed(evt);
Next is we need to identify who has been selected which is what we actually done on the mouse click we can just
copy the code and delete some of the requirements there.
Notice the changes from the line of codes
try{
int row = tbl_queryResultSet.getSelectedRow();
String row_user =( tbl_queryResultSet.getModel().getValueAt(row, 0).toString());
//String sql ="Select * from userAccounts where user_name='"+row_user+"'";
String sql ="DELETE from userAccounts where user_name='"+row_user+"'";
//----------------------------------------------------------------------------------------------------
SQLStatement=con.prepareStatement(sql);
//queryResultSet = SQLStatement.executeQuery(); instead of sending to the result set we will just execute
SQLStatement.execute();
//we will add the view action performed to refresh the table after deleting a data
this.btn_viewActionPerformed(evt);
//we will delete the whole if condition
// if (queryResultSet.next())
// {
// String user = queryResultSet.getString("user_name");
// txt_username.setText(user);
// String password = queryResultSet.getString("user_pass");
Prepared by: Engr. Louie Angelo M. Jalandoni
// txt_password.setText(password);
// }
}
catch(SQLException err)
{
javax.swing.JOptionPane.showMessageDialog(this,"Error:\n"+err);
}
So the code for the delete button will be like this
private void btn_deleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.btn_dbTesterActionPerformed(evt);
try{
int row = tbl_queryResultSet.getSelectedRow();
String row_user =( tbl_queryResultSet.getModel().getValueAt(row, 0).toString());
String sql ="DELETE from userAccounts where user_name='"+row_user+"'";
SQLStatement=con.prepareStatement(sql);
SQLStatement.execute();
this.btn_viewActionPerformed(evt);
}
catch(SQLException err)
{
javax.swing.JOptionPane.showMessageDialog(this,"Error:\n"+err);
}
}
Note: the codes is attached in the PDF file