-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShop.java
More file actions
240 lines (168 loc) · 6.41 KB
/
Copy pathShop.java
File metadata and controls
240 lines (168 loc) · 6.41 KB
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
//import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
//import java.awt.Color;
//import javax.swing.table.DefaultTableModel;
import net.proteanit.sql.DbUtils;
//import java.awt.Font;
//import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import javax.swing.ListSelectionModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
//import javax.swing.table.DefaultTableModel;
//import com.mysql.cj.xdevapi.Table;
public class Shop extends JFrame {
private JPanel contentPane;
private static final long serialVersionUID = 1L;
private static JTable table;
private JTextField prodname;
private JTextField prodcant;
private String prodid;
private String prodprice;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Shop frame = new Shop();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Shop() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 790, 487);
contentPane = new JPanel();
contentPane.setBackground(Color.CYAN);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(68, 285, 639, 152);
contentPane.add(scrollPane);
table = new JTable();
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setForeground(Color.BLACK);
table.setFont(new Font("Tahoma", Font.PLAIN, 15));
table.setBackground(Color.ORANGE);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
int index = table.getSelectedRow();
prodid=(model.getValueAt(index, 0).toString());
prodname.setText(model.getValueAt(index, 1).toString());
prodprice=(model.getValueAt(index, 2).toString());
}
});
scrollPane.setViewportView(table);
JButton btnBack = new JButton("Main menu");
btnBack.setForeground(Color.BLACK);
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Dashboard frame = new Dashboard();
frame.setVisible(true);
dispose();
}
});
btnBack.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
btnBack.setBackground(Color.RED);
btnBack.setBounds(568, 177, 139, 42);
contentPane.add(btnBack);
JLabel lblNewLabel_4 = new JLabel("PRODUCTS LIST");
lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_4.setFont(new Font("Copperplate Gothic Bold", Font.BOLD | Font.ITALIC, 15));
lblNewLabel_4.setBounds(309, 236, 150, 36);
contentPane.add(lblNewLabel_4);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shopy", "root", "esddg1312");
PreparedStatement sta2 = conn.prepareStatement("update prod set prodcant = prodcant - ? where prodid = ?");
sta2.setInt(1, Integer.valueOf(prodcant.getText()));
sta2.setInt(2, Integer.valueOf(prodid));
sta2.executeUpdate();
PreparedStatement sta = conn.prepareStatement("insert into cos values(?,?,?,?)");
sta.setInt(1, Integer.valueOf(prodid));
sta.setString(2, prodname.getText());
sta.setInt(3, Integer.valueOf(prodprice));
sta.setInt(4, Integer.valueOf(prodcant.getText()));
sta.executeUpdate();
JOptionPane.showMessageDialog(null, "New prod added!");
conn.close();
shopTable();
}
catch (Exception exc){
exc.printStackTrace();
}
}
});
btnAdd.setForeground(Color.BLACK);
btnAdd.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
btnAdd.setBackground(Color.RED);
btnAdd.setBounds(68, 177, 139, 42);
contentPane.add(btnAdd);
JLabel lblNewLabel_1 = new JLabel("ProdName");
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
lblNewLabel_1.setBounds(68, 72, 111, 36);
contentPane.add(lblNewLabel_1);
prodname = new JTextField();
prodname.setColumns(10);
prodname.setBounds(189, 74, 148, 36);
contentPane.add(prodname);
JLabel lblNewLabel_3 = new JLabel("ProdCant");
lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_3.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
lblNewLabel_3.setBounds(392, 72, 111, 36);
contentPane.add(lblNewLabel_3);
prodcant = new JTextField();
prodcant.setColumns(10);
prodcant.setBounds(518, 74, 148, 36);
contentPane.add(prodcant);
JLabel lblNewLabel_4_1 = new JLabel("SHOP");
lblNewLabel_4_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_4_1.setForeground(Color.RED);
lblNewLabel_4_1.setFont(new Font("Copperplate Gothic Bold", Font.BOLD | Font.ITALIC, 15));
lblNewLabel_4_1.setBounds(309, 11, 150, 36);
contentPane.add(lblNewLabel_4_1);
shopTable();
}
public void shopTable() {
try {
//Conection
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shopy", "root", "esddg1312");
//statement
String query="select * from prod";
PreparedStatement sta = myConn.prepareStatement(query);
//result
ResultSet myRes = sta.executeQuery();
table.setModel (DbUtils.resultSetToTableModel(myRes));
myConn.close();
}
catch (Exception exc){
exc.printStackTrace();
}
}
}