Hi i have a question how do i update database ,once i press on the field it gets active for adding :here is my form : DisplayRecord.png
And here my code so far ,i try to code the update button,but it does updates the field,but not the database.

package src;

import java.awt.HeadlessException;
import java.sql.*;
import javax.swing.JOptionPane;


public final class NavigateRecords extends javax.swing.JFrame {

    ResultSet resultSet;

    /** Creates new form NavigateRecords */
    public NavigateRecords() {
        initComponents();

        try {

              //create the connection object
              //ATTN: username and password must be changed depending on the settings on your database server
              Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "");

              //create a statement object.
          //We will use this object to carry our query to the database
          Statement statement = connection.createStatement();

              //exexute our query, which will lead to the return of a resultset
          resultSet = statement.executeQuery("SELECT * FROM authors");


              if (resultSet.next()) {
                loadRecord();
              } //end if

              else {
                JOptionPane.showMessageDialog(null, "There are no records in the database");
              } //end else
        }//end try

    catch(SQLException sqlex) {
            JOptionPane.showMessageDialog(null, sqlex.toString());
            System.exit(0);
    }
    }

    public void loadRecord() {

        try  {
            String authorsID = resultSet.getObject(1).toString();
            String authorsFirstName = resultSet.getObject(2).toString();
            String authorsSecondName = resultSet.getObject(3).toString();

            authorIDTextfield.setText(authorsID);
            fnameTextField.setText(authorsFirstName);
            lnameTextfield.setText(authorsSecondName);    
        }
        catch(Exception ex) {
            JOptionPane.showMessageDialog(null, "ERROR " + ex);
        }
}


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        recordsPanel = new javax.swing.JPanel();
        authorIDLabel = new javax.swing.JLabel();
        authorIDTextfield = new javax.swing.JTextField();
        fnameLabel = new javax.swing.JLabel();
        fnameTextField = new javax.swing.JTextField();
        lnameLabel = new javax.swing.JLabel();
        lnameTextfield = new javax.swing.JTextField();
        controlPanel = new javax.swing.JPanel();
        firstButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        previousButton = new javax.swing.JButton();
        lastButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        updateButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Display Records From Books Database");
        setMinimumSize(new java.awt.Dimension(250, 150));

        recordsPanel.setLayout(new java.awt.GridLayout(3, 2, 5, 5));

        authorIDLabel.setText("Author ID");
        recordsPanel.add(authorIDLabel);

        authorIDTextfield.setEditable(false);
        recordsPanel.add(authorIDTextfield);

        fnameLabel.setText("First Name");
        recordsPanel.add(fnameLabel);

        fnameTextField.setEditable(false);
        fnameTextField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                fnameTextFieldMousePressed(evt);
            }
        });
        recordsPanel.add(fnameTextField);

        lnameLabel.setText("Last Name");
        recordsPanel.add(lnameLabel);

        lnameTextfield.setEditable(false);
        recordsPanel.add(lnameTextfield);

        getContentPane().add(recordsPanel, java.awt.BorderLayout.CENTER);

        firstButton.setText("First");
        firstButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                firstButtonActionPerformed(evt);
            }
        });
        controlPanel.add(firstButton);

        nextButton.setText("Next >>");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });
        controlPanel.add(nextButton);

        previousButton.setText("<< Previous");
        previousButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousButtonActionPerformed(evt);
            }
        });
        controlPanel.add(previousButton);

        lastButton.setText("Last");
        lastButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lastButtonActionPerformed(evt);
            }
        });
        controlPanel.add(lastButton);

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });
        controlPanel.add(exitButton);

        updateButton.setText("Update");
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });
        controlPanel.add(updateButton);

        deleteButton.setText("Delete");
        controlPanel.add(deleteButton);

        getContentPane().add(controlPanel, java.awt.BorderLayout.SOUTH);

        setSize(new java.awt.Dimension(716, 338));
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try  {
            if (resultSet.next()) {
                loadRecord();
            }
            else {
                JOptionPane.showMessageDialog(null, "You have reached the end of the list");
                resultSet.last();
            }
        }//end try
        catch(Exception ex) {
            JOptionPane.showMessageDialog(null, ex.toString());
        }//end catch
    }                                          

    private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        try  {
            if (resultSet.previous()) {
                loadRecord();
            }
            else {
                JOptionPane.showMessageDialog(null, "You have reached the start of the list");
                resultSet.first();
            }
        }//end try
        catch(Exception ex) {
            JOptionPane.showMessageDialog(null, ex.toString());
        }//end catch
    }                                              

    private void firstButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        try {
            resultSet.first();
            loadRecord();
        } catch (SQLException ex) {
             JOptionPane.showMessageDialog(null, "ERROR" + ex);
        }
    }                                           

    private void lastButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
       try {
            resultSet.last();
            loadRecord();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "ERROR" + ex);
        }
    }                                          

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

        updateData();
        //loadRecord();

    }                                            

    private void updateData() throws HeadlessException {
        try  {


            String fname = fnameTextField.getText();

            fnameTextField.setText(fname);
            //lnameTextfield.setText(authorsSecondName);
        }
        catch(Exception ex) {
            JOptionPane.showMessageDialog(null, "ERROR " + ex);
        }
    }

    private void fnameTextFieldMousePressed(java.awt.event.MouseEvent evt) {                                            
        fnameTextField.setEditable(true);
    }                                           

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NavigateRecords().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel authorIDLabel;
    private javax.swing.JTextField authorIDTextfield;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JButton firstButton;
    private javax.swing.JLabel fnameLabel;
    private javax.swing.JTextField fnameTextField;
    private javax.swing.JButton lastButton;
    private javax.swing.JLabel lnameLabel;
    private javax.swing.JTextField lnameTextfield;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton previousButton;
    private javax.swing.JPanel recordsPanel;
    private javax.swing.JButton updateButton;
    // End of variables declaration                   

}

Edited 1 Year Ago by LibraryCode

learn about JDBC, or better yet JPA.

And do split your code into multiple classes, makes things so much easier to maintain and debug.
Also, keep class fields and methods separate, fields at the top, then constructors, then methods. Organised code is a lot easier to read.

This article has been dead for over six months. Start a new discussion instead.