001.
package
com.java.myapp;
002.
003.
import
java.sql.Connection;
004.
import
java.sql.DriverManager;
005.
import
java.sql.ResultSet;
006.
import
java.sql.SQLException;
007.
import
java.sql.Statement;
008.
import
javax.swing.JButton;
009.
import
javax.swing.JDialog;
010.
import
javax.swing.JOptionPane;
011.
import
javax.swing.JScrollPane;
012.
import
javax.swing.JTable;
013.
import
javax.swing.table.DefaultTableModel;
014.
import
java.awt.event.ActionListener;
015.
import
java.awt.event.ActionEvent;
016.
017.
public
class
MyCustomer
extends
JDialog {
018.
019.
public
String sCustomerID;
020.
public
String sName;
021.
public
String sEmail;
022.
public
String sCountryCode;
023.
public
String sBudget;
024.
public
String sUsed;
025.
026.
027.
028.
029.
public
static
void
main(String[] args) {
030.
try
{
031.
MyCustomer dialog =
new
MyCustomer();
032.
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
033.
dialog.setVisible(
true
);
034.
}
catch
(Exception e) {
035.
e.printStackTrace();
036.
}
037.
}
038.
039.
040.
041.
042.
public
MyCustomer() {
043.
setBounds(
100
,
100
,
564
,
241
);
044.
setTitle(
"Choose Customer"
);
045.
046.
getContentPane().setLayout(
null
);
047.
048.
049.
JScrollPane scrollPane =
new
JScrollPane();
050.
scrollPane.setBounds(
62
,
27
,
440
,
93
);
051.
getContentPane().add(scrollPane);
052.
053.
054.
final
JTable table =
new
JTable();
055.
scrollPane.setViewportView(table);
056.
057.
058.
DefaultTableModel model = (DefaultTableModel)table.getModel();
059.
model.addColumn(
"CustomerID"
);
060.
model.addColumn(
"Name"
);
061.
model.addColumn(
"Email"
);
062.
model.addColumn(
"CountryCode"
);
063.
model.addColumn(
"Budget"
);
064.
model.addColumn(
"Used"
);
065.
066.
Connection connect =
null
;
067.
Statement s =
null
;
068.
069.
try
{
070.
Class.forName(
"com.mysql.jdbc.Driver"
);
071.
073.
"?user=root&password=root"
);
074.
075.
s = connect.createStatement();
076.
077.
String sql =
"SELECT * FROM customer ORDER BY CustomerID ASC"
;
078.
079.
ResultSet rec = s.executeQuery(sql);
080.
int
row =
0
;
081.
while
((rec!=
null
) && (rec.next()))
082.
{
083.
model.addRow(
new
Object[
0
]);
084.
model.setValueAt(rec.getString(
"CustomerID"
), row,
0
);
085.
model.setValueAt(rec.getString(
"Name"
), row,
1
);
086.
model.setValueAt(rec.getString(
"Email"
), row,
2
);
087.
model.setValueAt(rec.getString(
"CountryCode"
), row,
3
);
088.
model.setValueAt(rec.getFloat(
"Budget"
), row,
4
);
089.
model.setValueAt(rec.getFloat(
"Used"
), row,
5
);
090.
row++;
091.
}
092.
093.
rec.close();
094.
095.
}
catch
(Exception e) {
096.
097.
JOptionPane.showMessageDialog(
null
, e.getMessage());
098.
e.printStackTrace();
099.
}
100.
101.
try
{
102.
if
(s !=
null
) {
103.
s.close();
104.
connect.close();
105.
}
106.
}
catch
(SQLException e) {
107.
108.
e.printStackTrace();
109.
}
110.
111.
112.
JButton btnOk =
new
JButton(
"OK"
);
113.
btnOk.addActionListener(
new
ActionListener() {
114.
public
void
actionPerformed(ActionEvent e) {
115.
int
index = table.getSelectedRow();
116.
sCustomerID = table.getValueAt(index,
0
).toString();
117.
sName = table.getValueAt(index,
1
).toString();
118.
sEmail = table.getValueAt(index,
2
).toString();
119.
sCountryCode = table.getValueAt(index,
3
).toString();
120.
sBudget = table.getValueAt(index,
4
).toString();
121.
sUsed = table.getValueAt(index,
5
).toString();
122.
dispose();
123.
}
124.
});
125.
btnOk.setBounds(
176
,
151
,
83
,
23
);
126.
getContentPane().add(btnOk);
127.
128.
129.
JButton btnCancel =
new
JButton(
"Cancel"
);
130.
btnCancel.addActionListener(
new
ActionListener() {
131.
public
void
actionPerformed(ActionEvent e) {
132.
dispose();
133.
}
134.
});
135.
btnCancel.setBounds(
292
,
151
,
89
,
23
);
136.
getContentPane().add(btnCancel);
137.
138.
}
139.
140.
}