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.SQLException;
007.
import
java.sql.Statement;
008.
import
javax.swing.JFrame;
009.
import
javax.swing.JLabel;
010.
import
javax.swing.JOptionPane;
011.
import
javax.swing.JButton;
012.
import
javax.swing.JTextField;
013.
014.
import
java.awt.event.ActionListener;
015.
import
java.awt.event.ActionEvent;
016.
017.
public
class
MyForm
extends
JFrame {
018.
019.
020.
021.
022.
public
static
void
main(String[] args) {
023.
EventQueue.invokeLater(
new
Runnable() {
024.
public
void
run() {
025.
MyForm frame =
new
MyForm();
026.
frame.setVisible(
true
);
027.
}
028.
});
029.
}
030.
031.
032.
033.
034.
public
MyForm() {
035.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
036.
setBounds(
100
,
100
,
449
,
303
);
037.
setTitle(
"ThaiCreate.Com Java GUI Tutorial"
);
038.
getContentPane().setLayout(
null
);
039.
040.
041.
JLabel hCustomerAdd =
new
JLabel(
"Customer Add"
);
042.
hCustomerAdd.setBounds(
144
,
21
,
132
,
14
);
043.
getContentPane().add(hCustomerAdd);
044.
045.
046.
JLabel hCustomerID =
new
JLabel(
"CustomerID :"
);
047.
hCustomerID.setBounds(
100
,
51
,
89
,
14
);
048.
getContentPane().add(hCustomerID);
049.
050.
JLabel hName =
new
JLabel(
"Name :"
);
051.
hName.setBounds(
100
,
76
,
89
,
14
);
052.
getContentPane().add(hName);
053.
054.
JLabel hEmail =
new
JLabel(
"Email :"
);
055.
hEmail.setBounds(
100
,
100
,
89
,
14
);
056.
getContentPane().add(hEmail);
057.
058.
JLabel hCountryCode =
new
JLabel(
"CountryCode :"
);
059.
hCountryCode.setBounds(
100
,
123
,
89
,
14
);
060.
getContentPane().add(hCountryCode);
061.
062.
JLabel hBudget =
new
JLabel(
"Budget :"
);
063.
hBudget.setBounds(
100
,
146
,
89
,
14
);
064.
getContentPane().add(hBudget);
065.
066.
JLabel hUsed =
new
JLabel(
"Used :"
);
067.
hUsed.setBounds(
100
,
171
,
89
,
14
);
068.
getContentPane().add(hUsed);
069.
070.
071.
072.
final
JTextField txtCustomerID =
new
JTextField(
""
);
073.
txtCustomerID.setBounds(
207
,
51
,
99
,
20
);
074.
getContentPane().add(txtCustomerID);
075.
076.
077.
final
JTextField txtName =
new
JTextField(
""
);
078.
txtName.setBounds(
207
,
76
,
99
,
20
);
079.
getContentPane().add(txtName);
080.
081.
082.
final
JTextField txtEmail =
new
JTextField(
""
);
083.
txtEmail.setBounds(
207
,
100
,
176
,
20
);
084.
getContentPane().add(txtEmail);
085.
086.
087.
final
JTextField txtCountryCode =
new
JTextField(
""
);
088.
txtCountryCode.setBounds(
207
,
123
,
99
,
20
);
089.
getContentPane().add(txtCountryCode);
090.
091.
092.
final
JTextField txtBudget =
new
JTextField(
""
);
093.
txtBudget.setBounds(
207
,
146
,
99
,
20
);
094.
getContentPane().add(txtBudget);
095.
096.
097.
final
JTextField txtUsed =
new
JTextField(
""
);
098.
txtUsed.setBounds(
207
,
171
,
99
,
20
);
099.
getContentPane().add(txtUsed);
100.
101.
102.
JButton btnSave =
new
JButton(
"Save"
);
103.
btnSave.addActionListener(
new
ActionListener() {
104.
public
void
actionPerformed(ActionEvent arg0) {
105.
106.
Connection connect =
null
;
107.
Statement s =
null
;
108.
109.
try
{
110.
Class.forName(
"com.mysql.jdbc.Driver"
);
111.
112.
connect = DriverManager.getConnection(
""
114.
+
"?user=root&password=root"
);
115.
116.
s = connect.createStatement();
117.
118.
119.
String sql =
"INSERT INTO customer "
120.
+
"(CustomerID,Name,Email,CountryCode,Budget,Used) "
121.
+
"VALUES ('"
+ txtCustomerID.getText() +
"','"
122.
+ txtName.getText() +
"','"
123.
+ txtEmail.getText() +
"'"
+
",'"
124.
+ txtCountryCode.getText() +
"','"
125.
+ txtBudget.getText() +
"','"
126.
+ txtUsed.getText() +
"') "
;
127.
s.execute(sql);
128.
129.
130.
txtCustomerID.setText(
""
);
131.
txtName.setText(
""
);
132.
txtEmail.setText(
""
);
133.
txtCountryCode.setText(
""
);
134.
txtBudget.setText(
""
);
135.
txtUsed.setText(
""
);
136.
137.
JOptionPane.showMessageDialog(
null
,
138.
"Record Inserted Successfully"
);
139.
140.
141.
}
catch
(Exception e) {
142.
143.
JOptionPane.showMessageDialog(
null
, e.getMessage());
144.
e.printStackTrace();
145.
}
146.
147.
try
{
148.
if
(s !=
null
) {
149.
s.close();
150.
connect.close();
151.
}
152.
}
catch
(SQLException e) {
153.
154.
System.out.println(e.getMessage());
155.
e.printStackTrace();
156.
}
157.
158.
}
159.
});
160.
btnSave.setBounds(
168
,
217
,
89
,
23
);
161.
getContentPane().add(btnSave);
162.
163.
}
164.
}