Java Database Connectivity : Updating & Deleting Data

Simple steps on how to update or delete data of a table through a GUI applications in NetBeans IDE.

1 First, follow the steps for searching the data through a GUI. This is to make sure that you are able to bring the data in the text-field for further updation or deletion.

Run View for Searching Data

2 Make sure you have made the text-fields as editable. To make them editable, select all the text-fields and right click. Now, select properties and checkmark(if, unchecked) the editable option.

3 Add a new button and rename it as “Update”. Now on the Update button’s actionperformed event

try {

        Connection con= (Connection)  DriverManager.getConnection(“jdbc:mysql://localhost:3306/database1″,”root”,””);
Statement stmt=con.createStatement();
        String num  = (String) jList1.getSelectedValue();
String no=jTextField1.getText();
        String title=jTextField2.getText();
        String auth=jTextField3.getText();
        String type=jTextField4.getText();
        String pub=jTextField5.getText();
        String qty=jTextField6.getText();
        String amt=jTextField7.getText(); 

String sql1 = “Update library set NO='” + (no)+ “‘, TITLE='” + (title)+ “‘, AUTHOR='” + (auth)+ “‘, TYPE='” + (type)+ “‘, PUB='” + (pub)+ “‘, QTY='” + (qty)+ “‘, AMOUNT='” + (amt)+ “‘ where NO = ‘” + (num) + “‘”;


catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());


4 To make your program more better you can make the text-fields empty when the user has completed task of updating his table. Also, you can show a Message Box stating that the update was successful.
To do this add the following codes after/below the line – stmt.executeUpdate(sql1);

JOptionPane.showMessageDialog(null, “Table LIBRARY Updated!”);

Run View for Updating Database

You can see that when a user clicks the update button the text-fields becomes empty and a message pops-ups stating that the table was updated successfully!

5 Now, for deleting a row from a database. Insert one more button and rename it to “Delete”.

6 On Delete button’s actionPerformed event

try {
Connection con= (Connection)  DriverManager.getConnection(“jdbc:mysql://localhost:3306/database1″,”root”,””);
Statement stmt=con.createStatement();
String num  = (String) jList1.getSelectedValue();

String sql2 = “Delete from library where NO = ‘”+(num)+”‘”;

JOptionPane.showMessageDialog(null, “Row deleted from the database!”);
catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
7 Here also I have used a JOptionPane to show a message to the user that the row was successfully deleted. Like the above message, you can also use various ways to make your program effective, example when a user clicks on delete button all the text fields gets cleared and also you can refresh the list to show that the deleted row does not exist any more in the database.
Run View for Delete

Remember to change the codes in blue according to your database and your GUI.

Please comment, if you are not able to follow.

//watch a video tutorial for java database connectivity on the videos page


28 thoughts on “Java Database Connectivity : Updating & Deleting Data”

  1. I have a hashmap(String, String)
    and I wanted to print its content in a jList or textbox for example. how do I do that?

  2. Hi,

    Thank you for your good work,

    I am having a problem here,
    String num = (String) jList1.getSelectedValue();

    since i am using Jtable to list my data, i changed the jlist1 to jtable and i am getting error on "getSelectedValue" please help.

  3. Sir! I'm fun of programming… and I have a hard time analyzing the code on how to clear a jTable by pressing a Clear button in my jFrame. we need your help! 🙂 TnX…!

  4. i have problem with this line..

    Statement stmt=con.createStatement(); //line no 173

    it gies error as
    at banking.CreateAcnt.jBcreateActionPerformed(
    at banking.CreateAcnt.access$000(

    i am implementing this code for creatining account for bank database.
    Please help me.

  5. i need to do following operation tat s to store ,modify,delete,display some data in java like form representation just like data as studentid ,name,phone no:

  6. thank you so much
    i have to mention that your blogs have been particularly helpful!
    great work sir, kudos!

    there's other problem i need help with…
    i need to fetch data from ms excel and place it into an oracle database.
    i am able to make connections to both these databases one at a time.

    is it possible to make multiple connections in the same code?
    please help!

  7. how can i select data first and update?
    I made like this: if(e.getSource()==b4)
    int i=0;
    Connection con=DriverManager.getConnection("jdbc:odbc:db1");
    ps1=con.prepareStatement("Select * from Details where Roll No=?");

    ResultSet rs=ps1.executeQuery();

    JOptionPane.showMessageDialog(null,"Roll no doesn't exist");
    catch(Exception e1)

  8. About the delete button i have an issue here . it tells me that it deleted the row but it does nothing . i select the row from a combobox

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

    try {
    Connection con= (Connection) DriverManager.getConnection("jdbc:sqlite:C:\users\Kajou\momentsdb.sqlite");
    Statement stmt=con.createStatement();
    String num = (String) comboBox1.getSelectedItem();

    String sql2 = "Delete from Table1 where rowid = '"+(num)+"'";
    JOptionPane.showMessageDialog(null, "Row deleted from the database!");
    catch(Exception e) {
    JOptionPane.showMessageDialog(this, e.getMessage());


Leave a Comment

Your email address will not be published. Required fields are marked *