Sunteți pe pagina 1din 11

Prepared by: Engr. Louie Angelo M.

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
EDIT

In editing a data first we need to save what has been edited so we will add three buttons
btn_edit, btn_save, and btn_cancel
btn_edit function will just allow us to edit the data in the textfield
the save button will allow us to save the data and the cancel button will revert all the changes

first is we will make the textfields disabled by default because at first we should not be able to edit the data, change
the property of the textfields to editable false as well as the save button and the cancel button into enabled false
You can find those properties in the properties window.
For the Edit button,
after clicking the edit button those text fields will be editable,
the edit button will be disabled
the save and cancel button will be enabled





Prepared by: Engr. Louie Angelo M. Jalandoni
private void btn_editActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txt_username.setEditable(true);
txt_password.setEditable(true);
btn_save.setEnabled(true);
btn_cancel.setEnabled(true);

// the others should be disabled
btn_edit.setEnabled(false);
btn_add.setEnabled(false);
btn_delete.setEnabled(false);
tbl_queryResultSet.setEnabled(false);
}

By default there should be a chosen value which is the first data being acquired, we will just copy the mouse clicked
program and set the row number to 0 and the column to 0. We will create the method defaultvalue to hold the
program
private void defaultvalue()
{
try {
String row_user =( tbl_queryResultSet.getModel().getValueAt(0, 0).toString());
String sql ="Select * from userAccounts where user_name='"+row_user+"'";
SQLStatement=con.prepareStatement(sql);
queryResultSet = SQLStatement.executeQuery();
if(queryResultSet.next())
{
String user = queryResultSet.getString("user_name");
txt_username.setText(user);
String password = queryResultSet.getString("user_pass");
txt_password.setText(password);

}

}
catch(SQLException err)
{

Prepared by: Engr. Louie Angelo M. Jalandoni
javax.swing.JOptionPane.showMessageDialog(this,"No data in the database"+err);
}

}
//--------------------------------------------------------------
Then add the call method in the constructor
this.defaultvalue
where the constructor will look like this
public DBTESTER() {
initComponents();
this.btn_viewActionPerformed(null);
this.defaultvalue();
}
//---------------------------------------------------------------------------------------------------------------
Once the cancel button is pressed everything should return in place the enable and disable as well
And we will again call the chosen row to return the previous value
private void btn_cancelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txt_username.setEditable(false);
txt_password.setEditable(false);
btn_save.setEnabled(false);
btn_cancel.setEnabled(false);
btn_edit.setEnabled(true);
btn_add.setEnabled(true);
btn_delete.setEnabled(true);
tbl_queryResultSet.setEnabled(true);
this.tbl_queryResultSetMouseClicked(null);
}

Prepared by: Engr. Louie Angelo M. Jalandoni
SAVE BUTTON
Once the button has been pressed everything will return to its state of enabled/disabled but before that we need to
update codes

To save the details we need to identify which of the rows has been selected to know what to update
Next is we need to get the value of the 2 text fields
Then we will integrate the query for the update command

Add again the query to connect
this.btn_dbTesterActionPerformed(evt);
create the try and catch that will hold the testing, if catch display cannot update data then the error
try{
}
catch(SQLException err)
{
javax.swing.JOptionPane.showMessageDialog(this,"Cannot Update the data:\n"+err);
}

Inside the try button we need to identify the selected row ( index/position )
int row = tbl_queryResultSet.getSelectedRow();
after that we need to get the updated values on the text boxes
String edituser = txt_username.getText();
String editpass = txt_password.getText();
Next will be we need to identify the username of the selected row by taking its value and saving to a string
String row_user =( tbl_queryResultSet.getModel().getValueAt(row, 0).toString());



Prepared by: Engr. Louie Angelo M. Jalandoni
We will now formulate the query, take note of the double quote and the single quote, all single quotation are inside
the double quotation, Also the proper spacing of the query
String sql ="UPDATE userAccounts SET "
+ "user_name = '"+edituser+"', "
+ "user_pass = '"+editpass+"' where user_name='"+row_user+"' ";
After we established the query we will now prepare it for execution
SQLStatement=con.prepareStatement(sql);
Then execute command because we will not return the value in the result set we will need to update the data
SQLStatement.execute();
Lastly, to view again all the data we need to call the btn_view to show everything in the table
this.btn_viewActionPerformed(evt);
After those commands we can now return the states to default
txt_username.setEditable(false);
txt_password.setEditable(false);
btn_save.setEnabled(false);
btn_cancel.setEnabled(false);
btn_edit.setEnabled(true);
btn_add.setEnabled(true);
btn_delete.setEnabled(true);
tbl_queryResultSet.setEnabled(true);
this.tbl_queryResultSetMouseClicked(null);
and if there is no error we will tell that the data has been saved
javax.swing.JOptionPane.showMessageDialog(this,"Data has been saved");




Prepared by: Engr. Louie Angelo M. Jalandoni
The Program for the saved button
private void btn_saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.btn_dbTesterActionPerformed(evt);
try{
int row = tbl_queryResultSet.getSelectedRow();
String edituser = txt_username.getText();
String editpass = txt_password.getText();

String row_user =( tbl_queryResultSet.getModel().getValueAt(row, 0).toString());
String sql ="UPDATE userAccounts SET "
+ "user_name = '"+edituser+"', "
+ "user_pass = '"+editpass+"' where user_name='"+row_user+"'";
SQLStatement=con.prepareStatement(sql);
SQLStatement.execute();
this.btn_viewActionPerformed(evt);
txt_username.setEditable(false);
txt_password.setEditable(false);

btn_save.setEnabled(false);
btn_cancel.setEnabled(false);
btn_edit.setEnabled(true);
btn_add.setEnabled(true);
btn_delete.setEnabled(true);
tbl_queryResultSet.setEnabled(true);
javax.swing.JOptionPane.showMessageDialog(this,"Data has been saved");
}
catch(SQLException err)
{
javax.swing.JOptionPane.showMessageDialog(this,"Cannot Update the data:\n"+err);
}
}


The problem here is we can no longer add up data since the textfield has been disabled
To resolve the problem we need to have a work around
That once the add button is pressed the textfield will be left blank and everything will be again disabled except from
the save button and cancel button.
This means that we will use the save button to ADD and as we did we will use it also to EDIT




Prepared by: Engr. Louie Angelo M. Jalandoni
First we will have a global declaration named savedState as String
These savedState will have a value of add or edit
Therefore once we pressed the save button we can now test if what button has been pressed
By default it will have a value of add and we need to insert it below the global variable queryResultSet
String savedState="add";
On the saved button
Enclosed the body of the program with a brace and make a condition
if(savedState.equals(edit))
thus we will have the following
if(savedState.equals("edit"))
{
this.btn_dbTesterActionPerformed(evt);
try{
int row = tbl_queryResultSet.getSelectedRow();
String edituser = txt_username.getText();
String editpass = txt_password.getText();

String row_user =( tbl_queryResultSet.getModel().getValueAt(row, 0).toString());
String sql ="UPDATE userAccounts SET "
+ "user_name = '"+edituser+"', "
+ "user_pass = '"+editpass+"' where user_name='"+row_user+"'";
SQLStatement=con.prepareStatement(sql);
SQLStatement.execute();
this.btn_viewActionPerformed(evt);
txt_username.setEditable(false);
txt_password.setEditable(false);

btn_save.setEnabled(false);
btn_cancel.setEnabled(false);
btn_edit.setEnabled(true);
btn_add.setEnabled(true);
btn_delete.setEnabled(true);
tbl_queryResultSet.setEnabled(true);
javax.swing.JOptionPane.showMessageDialog(this,"Data has been saved");
}
catch(SQLException err)
{
javax.swing.JOptionPane.showMessageDialog(this,"Cannot Update the data:\n"+err);
}//end of catch

}//end of if


Prepared by: Engr. Louie Angelo M. Jalandoni
Next is we will have an else condition that will hold the program in the add button, you will just cut the whole body of
the program in the add and transfer that into the saved button enclosed by an else condition.
Additional after saving the data we need to change the state again into its default state as well as call the default
value for the textfields.

else
{
this.btn_dbTesterActionPerformed(evt);
try{
//Inserting the data
String sql = "INSERT INTO useraccounts values (?,?)";
SQLStatement= con.prepareStatement(sql);
SQLStatement.setString(1, txt_username.getText());
SQLStatement.setString(2, txt_password.getText());
SQLStatement.execute();
javax.swing.JOptionPane.showMessageDialog(this,"Data has been saved");
this.btn_viewActionPerformed(evt);

// default
txt_username.setEditable(false);
txt_password.setEditable(false);
btn_save.setEnabled(false);
btn_cancel.setEnabled(false);
btn_edit.setEnabled(true);
btn_add.setEnabled(true);
btn_delete.setEnabled(true);
tbl_queryResultSet.setEnabled(true);
this.defaultvalue();
}
catch ( SQLException | HeadlessException err ) {
javax.swing.JOptionPane.showMessageDialog(this,"Error Saving Data:\n"+err);
}
}//end of else

After that we will add another command on the edit button that once it has been pressed the value of the savedState
will be edit so as for the add button once pressed the value will be add

Therefore the edit button will look like this

private void btn_editActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txt_username.setEditable(true);
txt_password.setEditable(true);
btn_save.setEnabled(true);
btn_cancel.setEnabled(true);
btn_edit.setEnabled(false);
btn_add.setEnabled(false);
btn_delete.setEnabled(false);
tbl_queryResultSet.setEnabled(false);
savedState ="edit";
}

Prepared by: Engr. Louie Angelo M. Jalandoni

for the add button, that will be almost the same only for the savedState value and we will also blanked the textfields

private void btn_addActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
txt_username.setEditable(true);
txt_password.setEditable(true);
btn_save.setEnabled(true);
btn_cancel.setEnabled(true);
btn_edit.setEnabled(false);
btn_add.setEnabled(false);
btn_delete.setEnabled(false);
tbl_queryResultSet.setEnabled(false);
savedState ="add";
txt_username.setText("");
txt_password.setText("");
}


Sample Output
Upon running











Prepared by: Engr. Louie Angelo M. Jalandoni


When the add button is pressed




After Typing





Prepared by: Engr. Louie Angelo M. Jalandoni
After clicking the saved button













Note: the codes is attached in the PDF file

S-ar putea să vă placă și