Java GUI Programming

Update data

In previous example you have created a simple application. Now we will see how to update inserted data using java program. I assume that you all have downloaded my second example. Because I am going to use that example today onwards. Now you know how to add data. What happen when you insert incorrect data? So you must be able to update your data.

To do that we can use UPDATE command in MySQL command prompt.But our task is not that below code will show how to update data.First download and open this exaple and open it.First of all you have to create a new database and create column in appropriate way. I have created a database called “contact” and table “contactinfromation”. Then create rows

ContactId int notnull auto increment type,title,fName,lName,nName,email rows in string format.

Otherwise it will create error message when you run the program.You can restore the this backup file using mysql-gui tools.contact 20101201 1940.sql I am not going to explain everything but i will explain essential parts.

public class updateData { 

connection getcon =new connection();

public void updateData(String id,String title,String fname,String lname,String nname,String mail){
try {

getcon.creatConnection().createStatement().executeUpdate("update contactinformation set title='" + title+ "' ,fName ='"+ fname+"',lName='" + lname+ " ', nName= ' " + nname+ " ' ,email='" + mail+ "' where contactId="+ id +" ");

JOptionPane.showMessageDialog(null, "Income details updated succesfully");//confirmation message

}
catch(Exception ex){

JOptionPane.showMessageDialog(null, "Error.."+ex.getMessage());//handling exception

}

}

}

After update data it will give you a message.

Now we have created a method to update data and inside the click event of the button we have to use that method.Code you can see in below

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) { 

updateData upd= new updateData();

upd.updateData(txtId.getText().trim(), txtTitle.getText().trim(), txtFirstname.getText().trim(), txtLastname.getText().trim(),txtNickname.getText().trim(), txtEmail.getText().trim());
txtEmail.setText(null);
txtFirstname.setText(null);
txtId.setText(null);
txtLastname.setText(null);
txtNickname.setText(null);
txtTitle.setText(null);

txtId.requestFocus();

// TODO add your handling code here:
}

That is how we use Java and MySQL syntaxes to update data.

DOWNLOAD EXAMPLE HERE

8 thoughts on “Update data

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s