Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
No Result
View All Result
ADVERTISEMENT
Home Java

Java MySQL JList – INSERT SELECT UPDATE DELETE

2 years ago
in Java
Reading Time: 5min read
38 3
A A
41
SHARES
4.1k
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

 

Java MySQL JList – INSERT SELECT UPDATE DELETE Tutorial.

SimilarPosts

Java Iterator

Java Iterator

2 years ago
4k
Java Iterable Interface

Java Iterable Interface

2 years ago
4k
Java Control Structures – Loops Introduction

Java Control Structures – Loops Introduction

2 years ago
4k
Java Operators – Logical Operators

Java Operators – Logical Operators

2 years ago
4k

This is an Java MySQL JList example .Its a simple CRUD app.We shall save items to MySQL,retrieve,update and delete.We shall be using JTextfields,JButtons and JList.We used netbeans in this tutorial.

What we do :

  • Connect to MySQL using JDBC.
  • User types data into JTextField and clicks save button.
  • We then save this data into MySQL.
  • We retrieve after saving and bind our data to JList.
  • As user enters data he can see the changes he’s made.
  • User can select an item from JList.
  • It gets set to the corresponding jtextfield.
  • He can change and click update to update.
  • Or he can delete and it gets deleted from MySQL database.

===

SECTION 1 : Our Database Class

Database CRUD class

Main Responsibility : Perform all CRUD operations.

  • INSERTS/SAVES data to MySQL database.
  • SELECTS/RETRIEVES data to MySQL.
  • UPDATES/EDITS data.
  • DELETES data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package jlist.database;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.DefaultListModel;
 
public class DBClass {
 
    String conString = "jdbc:mysql://localhost:3306/playersdb";
    String username = "root";
    String password = "";
 
    //INSERT INTO DB
    public Boolean add(String name) {
        //SQL STMT
        String sql = "INSERT INTO playerstb(Name) VALUES('" + name + "')";
 
        try {
            //GET COONECTION
            Connection con = DriverManager.getConnection(conString, username, password);
 
            // PREPARED STMT
            Statement s = con.prepareStatement(sql);
 
            //EXECUTE
            s.execute(sql);
 
            return true;
 
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
 
    }
 
    //RETRIEVE DATA
    public DefaultListModel retrieve() {
        DefaultListModel dm = new DefaultListModel();
 
        //SQL STMT
        String sql = "SELECT Name FROM playerstb";
 
        try {
            Connection con = DriverManager.getConnection(conString, username, password);
 
            //PREPARED STMT
            Statement s = con.prepareStatement(sql);
            ResultSet rs = s.executeQuery(sql);
 
            //LOOP THRU GETTING ALL VALUES
            while (rs.next()) {
                //GET VALUES
                String name = rs.getString(1);
 
                //ADD TO DM
                dm.addElement(name);
            }
 
            return dm;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
 
        return null;
    }
 
    //UPDATE DATA
    public Boolean update(String id, String value) {
        String sql = "UPDATE playerstb SET Name ='" + value + "' WHERE Name='" + id + "'";
 
        try {
            Connection con=DriverManager.getConnection(conString, username, password);
 
            //STATEMENT
            Statement s=con.prepareStatement(sql);
 
            //EXECUTE
            s.execute(sql);
 
            return true;
 
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
 
   //DELETE DATA
    public Boolean delete(String id)
    {
        //SQL STMT
        String sql="DELETE FROM playerstb WHERE Name ='"+id+"'";
 
        try
        {
            //CONNECTION
             Connection con=DriverManager.getConnection(conString, username, password);
 
             //sTAETEMT
             Statement s=con.prepareStatement(sql);
 
             //EXECUTE
             s.execute(sql);
 
             return true;
 
        }catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
    }
 
}

SECTION 2 : Our GUI Class

GUI and Main class

Main Responsibility : STARTS OUR APPLICATION

ADVERTISEMENT
  • Is our main class.
  • Derives from JFrame.
  • Handles all GUI interactions such as input of data.
  • Instantiates Database class and invokes its database manipulation methods.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package jlist.database;
 
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
 
public class GUI_Jlist extends javax.swing.JFrame {
 
    String id="";
 
    public GUI_Jlist() {
        initComponents();
    }
 
    private void retrieve()
    {
        DefaultListModel dm=new DBClass().retrieve();
 
        jList1.setModel(dm);
    }
 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
 
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        nameTxt = new javax.swing.JTextField();
        addBtn = new javax.swing.JButton();
        updateBtn = new javax.swing.JButton();
        retrieveBtn = new javax.swing.JButton();
        deleteBtn = new javax.swing.JButton();
        clearBtn = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jLabel2.setText("ProgrammingWizards Channel");
 
        jPanel1.setBackground(new java.awt.Color(45, 155, 193));
 
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jList1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
 
        jLabel1.setText("Name");
 
        addBtn.setText("Add");
        addBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBtnActionPerformed(evt);
            }
        });
 
        updateBtn.setText("Update");
        updateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateBtnActionPerformed(evt);
            }
        });
 
        retrieveBtn.setText("Retrieve");
        retrieveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                retrieveBtnActionPerformed(evt);
            }
        });
 
        deleteBtn.setText("Delete");
        deleteBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteBtnActionPerformed(evt);
            }
        });
 
        clearBtn.setText("Clear");
        clearBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearBtnActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(475, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(retrieveBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteBtn)
                        .addGap(35, 35, 35))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(clearBtn)
                        .addGap(78, 78, 78))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addBtn)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addComponent(jLabel1)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addComponent(updateBtn))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(nameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(102, 102, 102)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(147, 147, 147)
                            .addComponent(jLabel2))
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(224, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(131, 131, 131)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addBtn)
                    .addComponent(updateBtn))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(retrieveBtn)
                    .addComponent(deleteBtn))
                .addGap(43, 43, 43)
                .addComponent(clearBtn)
                .addContainerGap(125, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(25, 25, 25)
                    .addComponent(jLabel2)
                    .addGap(8, 8, 8)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(25, 25, 25)))
        );
 
        pack();
    }// </editor-fold>
 
    private void jList1MouseClicked(java.awt.event.MouseEvent evt) {
       id=jList1.getSelectedValue().toString();
       nameTxt.setText(id);
    }
 
    //ADD
    private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {
              if(new DBClass().add(nameTxt.getText()))
              {
                  JOptionPane.showMessageDialog(null, "Successfully Inserted");
                  nameTxt.setText("");
 
                  retrieve();
              }else
              {
                  JOptionPane.showMessageDialog(null, "Not Inserted");
              }
    }
 
    //UPDATE
    private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {
 
              if(new DBClass().update(id,nameTxt.getText()))
              {
                  JOptionPane.showMessageDialog(null, "Successfully Updated");
                  nameTxt.setText("");
 
                  retrieve();
              }else
              {
                  JOptionPane.showMessageDialog(null, "Not Updated");
              }
    }
 
    //RETRIEVE
    private void retrieveBtnActionPerformed(java.awt.event.ActionEvent evt) {
       retrieve();
    }
 
    //DELETE
    private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {
 
              if(new DBClass().delete(id))
              {
                  JOptionPane.showMessageDialog(null, "Successfully Deleted");
                  nameTxt.setText("");
 
                  retrieve();
              }else
              {
                  JOptionPane.showMessageDialog(null, "Not Deleted");
              }
    }
 
    //CLEAR
    private void clearBtnActionPerformed(java.awt.event.ActionEvent evt) {
        jList1.setModel(new DefaultListModel());
    }
 
    /
     * @param args the command line arguments
     */
    public static void main(String args[]) {
 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI_Jlist().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify
    private javax.swing.JButton addBtn;
    private javax.swing.JButton clearBtn;
    private javax.swing.JButton deleteBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nameTxt;
    private javax.swing.JButton retrieveBtn;
    private javax.swing.JButton updateBtn;
    // End of variables declaration
}
 

LAST SECTION

  • Lets share more tips in OUR FB PAGE.
  • To see the XML  we were parsing and the website itself please have look at the tutorial at our youtbe channel : ProgramminWizards.
  • You’ll also find the demo for this example and step by step explanations.
ADVERTISEMENT
ADVERTISEMENT
Share16Tweet10Send
ADVERTISEMENT

Related Posts

Java Iterator
Java

Java Iterator

December 19, 2020
4k
Java Iterable Interface
Java

Java Iterable Interface

December 19, 2020
4k
Java Control Structures – Loops Introduction
Java

Java Control Structures – Loops Introduction

December 19, 2020
4k
Java Operators – Logical Operators
Java

Java Operators – Logical Operators

December 19, 2020
4k
Java Operators – Relational Operators
Java

Java Operators – Relational Operators

December 19, 2020
4k
Java URI
Java

Java URI

December 19, 2020
4k
ADVERTISEMENT

Get Free Projects

  • Home
  • Android
  • Buy Projects
  • My Account

© 2021 Camposha

No Result
View All Result
  • Account
  • Activate
  • Activity
  • Become a Teacher
  • Become a Teacher
  • Become a Teacher
  • Become instructor
  • Blog
  • Blog
  • Cancel Payment
  • Cancel Payment
  • Cart
  • Change Password
  • Change Password
  • Checkout
  • Checkout
  • Checkout
  • Contact
  • Contact
  • Contact Us
  • Content restricted
  • Course Checkout
  • Dashboard
  • Edit Profile
  • Edit Profile
  • FAQs
  • Forgot Password
  • Forgot Password
  • Guest
  • Guest
  • Home
  • Home
  • Home Light
  • Instructor Dashboard
  • Instructor Registration
  • IUMP – Account Page
  • IUMP – Default Redirect Page
  • IUMP – Login
  • IUMP – LogOut
  • IUMP – Register
  • IUMP – Reset Password
  • IUMP – TOS Page
  • IUMP – Visitor Inside User Page
  • List courses
  • List wish list
  • Login
  • Login
  • Maintenance
  • Members
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Plans
  • My Account
  • OnePage Documentation
  • Portfolio Grid
  • Portfolio Masonry
  • Portfolio Multigrid
  • Privacy Policy
  • Products
  • Profile
  • Profile
  • Profile
  • Projects
  • Register
  • Register
  • Register
  • Register
  • Sample Page
  • Shop
  • Sign in
  • Sign up
  • Student profile
  • Student Registration
  • Thank You
  • Thank You

© 2021 Camposha

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In