001.
package
com.java.myapp;
002.
003.
import
java.awt.EventQueue;
004.
import
java.sql.Connection;
005.
import
java.sql.DriverManager;
006.
import
java.sql.ResultSet;
007.
import
java.sql.SQLException;
008.
import
java.sql.Statement;
009.
import
javax.swing.JFrame;
010.
import
javax.swing.JOptionPane;
011.
import
javax.swing.JTable;
012.
import
javax.swing.JScrollPane;
013.
import
javax.swing.table.DefaultTableModel;
014.
import
javax.swing.JLabel;
015.
import
javax.swing.JButton;
016.
import
java.awt.event.ActionListener;
017.
import
java.awt.event.ActionEvent;
018.
019.
public
class
MyForm
extends
JFrame {
020.
021.
static
JTable table;
022.
023.
024.
025.
026.
public
static
void
main(String[] args) {
027.
EventQueue.invokeLater(
new
Runnable() {
028.
public
void
run() {
029.
MyForm frame =
new
MyForm();
030.
frame.setVisible(
true
);
031.
}
032.
});
033.
}
034.
035.
036.
037.
038.
public
MyForm() {
039.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
040.
setBounds(
100
,
100
,
516
,
319
);
041.
setTitle(
"ThaiCreate.Com Java GUI Tutorial"
);
042.
getContentPane().setLayout(
null
);
043.
044.
045.
JLabel lblCustomerList =
new
JLabel(
"Customer List"
);
046.
lblCustomerList.setBounds(
207
,
44
,
87
,
14
);
047.
getContentPane().add(lblCustomerList);
048.
049.
050.
JScrollPane scrollPane =
new
JScrollPane();
051.
scrollPane.setBounds(
28
,
84
,
440
,
89
);
052.
getContentPane().add(scrollPane);
053.
054.
055.
table =
new
JTable();
056.
scrollPane.setViewportView(table);
057.
058.
059.
JButton btnDelete =
new
JButton(
"Delete"
);
060.
btnDelete.addActionListener(
new
ActionListener() {
061.
public
void
actionPerformed(ActionEvent e) {
062.
063.
Object[] options = {
"Yes"
,
"No"
};
064.
int
n = JOptionPane
065.
.showOptionDialog(
null
,
"Do you want to Delete data?"
,
066.
"Confirm to Delete?"
,
067.
JOptionPane.YES_NO_CANCEL_OPTION,
068.
JOptionPane.QUESTION_MESSAGE,
null
, options,
069.
options[
1
]);
070.
if
(n ==
0
)
071.
{
072.
073.
for
(
int
i =
0
; i < table.getRowCount(); i++) {
074.
Boolean chkDel = Boolean.valueOf(table.getValueAt(i,
0
).toString());
075.
if
(chkDel)
076.
{
077.
String strCustomerID = table.getValueAt(i,
1
)
078.
.toString();
079.
080.
DeleteData(strCustomerID);
081.
}
082.
}
083.
084.
JOptionPane.showMessageDialog(
null
,
"Delete Data Successfully"
);
085.
086.
PopulateData();
087.
}
088.
089.
}
090.
});
091.
btnDelete.setBounds(
205
,
202
,
89
,
23
);
092.
getContentPane().add(btnDelete);
093.
094.
PopulateData();
095.
}
096.
097.
private
static
void
PopulateData() {
098.
099.
100.
table.setModel(
new
DefaultTableModel());
101.
102.
103.
DefaultTableModel model =
new
DefaultTableModel() {
104.
105.
public
Class<?> getColumnClass(
int
column) {
106.
switch
(column) {
107.
case
0
:
108.
return
Boolean.
class
;
109.
case
1
:
110.
return
String.
class
;
111.
case
2
:
112.
return
String.
class
;
113.
case
3
:
114.
return
String.
class
;
115.
case
4
:
116.
return
String.
class
;
117.
case
5
:
118.
return
String.
class
;
119.
case
6
:
120.
return
String.
class
;
121.
default
:
122.
return
String.
class
;
123.
}
124.
}
125.
};
126.
table.setModel(model);
127.
128.
129.
model.addColumn(
"Select"
);
130.
model.addColumn(
"CustomerID"
);
131.
model.addColumn(
"Name"
);
132.
model.addColumn(
"Email"
);
133.
model.addColumn(
"CountryCode"
);
134.
model.addColumn(
"Budget"
);
135.
model.addColumn(
"Used"
);
136.
137.
Connection connect =
null
;
138.
Statement s =
null
;
139.
140.
try
{
141.
Class.forName(
"com.mysql.jdbc.Driver"
);
142.
143.
connect = DriverManager
145.
+
"?user=root&password=root"
);
146.
147.
s = connect.createStatement();
148.
149.
String sql =
"SELECT * FROM customer ORDER BY CustomerID ASC"
;
150.
151.
ResultSet rec = s.executeQuery(sql);
152.
int
row =
0
;
153.
while
((rec !=
null
) && (rec.next())) {
154.
model.addRow(
new
Object[
0
]);
155.
model.setValueAt(
false
, row,
0
);
156.
model.setValueAt(rec.getString(
"CustomerID"
), row,
1
);
157.
model.setValueAt(rec.getString(
"Name"
), row,
2
);
158.
model.setValueAt(rec.getString(
"Email"
), row,
3
);
159.
model.setValueAt(rec.getString(
"CountryCode"
), row,
4
);
160.
model.setValueAt(rec.getFloat(
"Budget"
), row,
5
);
161.
model.setValueAt(rec.getFloat(
"Used"
), row,
6
);
162.
row++;
163.
}
164.
165.
}
catch
(Exception e) {
166.
167.
JOptionPane.showMessageDialog(
null
, e.getMessage());
168.
e.printStackTrace();
169.
}
170.
171.
try
{
172.
if
(s !=
null
) {
173.
s.close();
174.
connect.close();
175.
}
176.
}
catch
(SQLException e) {
177.
178.
e.printStackTrace();
179.
}
180.
}
181.
182.
183.
private
void
DeleteData(String strCustomerID) {
184.
185.
Connection connect =
null
;
186.
Statement s =
null
;
187.
188.
try
{
189.
Class.forName(
"com.mysql.jdbc.Driver"
);
190.
191.
connect = DriverManager
193.
+
"?user=root&password=root"
);
194.
195.
s = connect.createStatement();
196.
197.
String sql =
"DELETE FROM customer WHERE "
+
"CustomerID = '"
198.
+ strCustomerID +
"' "
;
199.
s.execute(sql);
200.
201.
}
catch
(Exception e) {
202.
203.
JOptionPane.showMessageDialog(
null
, e.getMessage());
204.
e.printStackTrace();
205.
}
206.
207.
try
{
208.
if
(s !=
null
) {
209.
s.close();
210.
connect.close();
211.
}
212.
}
catch
(SQLException e) {
213.
214.
System.out.println(e.getMessage());
215.
e.printStackTrace();
216.
}
217.
218.
}
219.
220.
}