Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.
Lesson 54: How to code for table update button in Java NetBeans
In the previous lesson, we covered on how to code for table navigation buttons; First, Next, Previous and Last table navigation buttons in Java NetBeans. In this lesson, we shall learn how to code for table update button.
Table manipulation in Java involves actions that can change the status of a record or file such as Adding new record, Delete, Update etc. While some action such as update may affect a single table field others such as delete affects the entire records.
How To Update A Record In Java NetBeans
We’ll begin by implementing a code to update a record in a table. For any change made in a table field, this change should be reflected in the table in form of updates.
Java ResultSet has update methods that can be used to update records both in ResultSet and the database.
Modify the Java form we had in the previous lesson to include an Update button.
You might need to enlarge the size of the form to accommodate all the buttons. Change the button caption to Update and variable name to your preferred variable name. Here is the current Java form now.
To update a record in Java NetBeans, we need to get the current values in the form text fields and replace table fields with these values. Here is the code to get all the values from the form text fields.
String upId = txtID.getText( );
String upName = txtName.getText( );
String upFaculty = txtFaculty.getText( );
String upCourse = txtCourse.getText( );
String upCity = txtCity.getText( );
String upAddress = txtAddress.getText( );
String upTelephone = txtTelephone.getText( );
String upRem = txtRem.getText( );
Remember, our table has one integer field (StdID) while all the others are of string type.
We need to convert the variable upId (Currently in string type) to integer type before we can update the table. Below is the code statement to do so.
int upId2 = Integer.parseInt( upId ); //Type as one line
Now that we have the correct values ready in variables we can go ahead and update the table.
The ResultSet has various methods of updating a table. If we want to update string fields, we use updateString() while updateInt() is for integer fields.
The update methods will only update the ResultSet so, to commit updates to the database table we also issue updateRow() method.
The update code should be inside try … catch block. Here is the complete code in Update button.
Run the form now, you should be able to update a record. From the image below, notice the name in the first record has been updated from “Ephim Mada” to “Ephim Monday” with an update message in a dialog box.
Code For Java Form Update Button
So, to update a table in Java NetBeans, we just get the current values in the form and replace the table record with these new values and then commit the update action. Remember, if you have table fields of integer type you’ll need to convert string values to integer type first before you update. In the next lesson, we shall learn how to code for Cancel and New buttons in Java NetBeans programming.
Other related Hubs...
- Working with Java NetBeans GUI controls: Java Menus
A Menu is a navigation control that enables users to access application options in a hierarchical manner. Each menu item may contain a menu list of items which subsequently may contain sub-items and so on. Menus come in different forms and functional
- How To Write A Program In Java NetBeans
How to write a program in Java NetBeans is the first step in Java programming for beginners. This tutorial explains how to write a program in Java NetBeans programming environment.
- How to Use Java Save File dialog box to save files
Java Save dialog box, unlike Java Open dialog box, is used to display various computer locations where users can save files. This article discusses how to implement Java Save dialog box in Java NetBeans programming environment.
- How To Work With User Inputs In Java NetBeans
This tutorial discusses how to handle user inputs in Java NetBeans using the Scanner class of the Java library and how get and display user inputs in Java NetBeans programming.
Want to make money online?
This content is accurate and true to the best of the author’s knowledge and is not meant to substitute for formal and individualized advice from a qualified professional.
© 2013 Danson Wachira
Trang on January 20, 2014:
Thanks for your e-book. It's very helpful for me.
Mahendra on November 19, 2013:
I have recently gone through your JDBC and it was amazingly helpful to me
Danson Wachira (author) from Nairobi, Kenya on April 30, 2013:
You are the fourth Hubber to front an idea of an e-book to me, i have been thinking about it of late. Thanks for the visit and comment.
kidscrafts from Ottawa, Canada on April 30, 2013:
Your course is very well organized! I hope that you will publish some e-book later on with all your materials; it will help a lot of people and you will make some money of it :-)