Display the Database Records – Java Swing

Hello guys, Bushan here, welcome back to B2 Tech. In our previous article, we have discussed about Inserting values to MySQL Database. In this article, let’s display the database values in a JFrame form so let’s get started.



The first step is to drag and drop 3 labels and  1 button on to your JFrame form, give a unique name to all 3 labels, label_id, label_name, label_age



Now double-click on the Display button it will create an ActionPerformed event, and write the following code

try{
            String sql = "select * from details";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while(rs.next()){
                String a = rs.getString("sid");
                label_id.setText(a);
                String b = rs.getString("sname");
                label_name.setText(b);
                String c = rs.getString("sage");
                label_age.setText(c);
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }

At this point, if you run this application you will see the JFrame form and Display button when you click the Display button the last record in the database will be displayed on the labels



So now let’s see how to clear the textbox/textfield values with a single click. To do that first, let’s drag and drop a button on to the frame and change the text to Reset

After the constructor, let’s write the following code,

public void reset(){
    txt_id.setText("");
    txt_name.setText("");
    txt_age.setText("");
}

Now double-click on the reset button, call the reset() method inside of it,



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

Call the same reset() method even after the insert operation, so that it will clear all the textbox values after inserting a new record. Here is the complete source code,



public class login extends javax.swing.JFrame {

    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    public login() {
        initComponents();
        conn = connection.connectdb();
    }
    
    public void reset(){
        txt_id.setText("");
        txt_name.setText("");
        txt_age.setText("");
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try{
            String sql = "insert into details(sid, sname, sage) values(?, ?, ?)";
            pst = conn.prepareStatement(sql);
            pst.setString(1, txt_id.getText());
            pst.setString(2, txt_name.getText());
            pst.setString(3, txt_age.getText());
            pst.execute();
            JOptionPane.showMessageDialog(null, "Inserted successfully!");
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
        reset();
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try{
            String sql = "select * from details";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while(rs.next()){
                String a = rs.getString("sid");
                label_id.setText(a);
                String b = rs.getString("sname");
                label_name.setText(b);
                String c = rs.getString("sage");
                label_age.setText(c);
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }                                        

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



 

About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

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