-
Notifications
You must be signed in to change notification settings - Fork 2
/
GuiQuery9.java
277 lines (260 loc) · 9.83 KB
/
GuiQuery9.java
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package dbms;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
* Created by Saksham on 11/29/2016.
*/
public class GuiQuery9 {
JTextField SearchMed=new JTextField(10);
JButton find=new JButton("Find");
JTextField TotalCost=new JTextField(10);
JTextField DistributorId=new JTextField(10);
JTextField DrugId = new JTextField(15);
JTextField Quantity = new JTextField(15);
JPanel base=new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton reset=new JButton("Reset");
JButton search=new JButton("Submit");
JButton addcart=new JButton("Cart");
private DefaultTableModel model = new DefaultTableModel();
JTable table;
JPanel centre;
public void query9gui(JPanel westbottom,JPanel centr){
westbottom.removeAll();
this.centre=centr;
centre.removeAll();
westbottom.setLayout(new BoxLayout(westbottom, BoxLayout.Y_AXIS));
JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT));
p1.add(new JLabel("Distributor Id"));
p1.add(DistributorId);
JPanel p2=new JPanel(new FlowLayout(FlowLayout.LEFT));
p2.add(new JLabel("Drug Id"));
p2.add(DrugId);
JPanel p3=new JPanel(new FlowLayout(FlowLayout.LEFT));
p3.add(new JLabel("Quantity"));
p3.add(Quantity);
JPanel p5=new JPanel(new FlowLayout(FlowLayout.LEFT));
p5.add(new JLabel("Give Med Name"));
p5.add(SearchMed);
find.addActionListener(new find());
p5.add(find);
westbottom.add(p1);
westbottom.add(p2);
westbottom.add(p3);
search.addActionListener(new submit());
reset.addActionListener(new reset());
addcart.addActionListener(new addtocart());
base.add(search);
base.add(reset);
base.add(addcart);
westbottom.add(base);
centre.removeAll();
centre.repaint();
centre.revalidate();
westbottom.revalidate();
westbottom.repaint();
model.addColumn("Drug Name");
model.addColumn("Drug Id");
model.addColumn("Quantity");
table = new JTable(model);
centre.setLayout(new BorderLayout());
centre.add(p5, BorderLayout.NORTH);
centre.add( new JScrollPane( table ), BorderLayout.CENTER );
centre.revalidate();
}
//seteditable
public void reset(){
centre.removeAll();
JPanel p5=new JPanel(new FlowLayout(FlowLayout.LEFT));
p5.add(new JLabel("Give Med Name"));
p5.add(SearchMed);
p5.add(find);
DistributorId.setText("");
DrugId.setText("");
Quantity.setText("");
model = new DefaultTableModel();
model.addColumn("Drug Name");
model.addColumn("Drug Id");
model.addColumn("Quantity");
table = new JTable(model);
centre.setLayout(new BorderLayout());
centre.add(p5,BorderLayout.NORTH);
centre.add( new JScrollPane( table ), BorderLayout.CENTER );
centre.repaint();
centre.revalidate();
}
public void runQuery(){
Integer sale_id = 1;
Connection connect = null;
Statement statement = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//DefaultTableModel mod=(DefaultTableModel)table.getModel();
try{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/dbms?"+"user=root&password=pass");
statement = connect.createStatement();
resultSet = statement.executeQuery("SELECT * FROM Purchase ORDER BY Purchase_ID DESC LIMIT 1");
if(!resultSet.isBeforeFirst()){
sale_id = 1;
}else{
resultSet.next();
sale_id=Integer.parseInt(resultSet.getString("Purchase_ID").substring(3))+1;
}
resultSet = statement.executeQuery("SELECT * FROM Distributor WHERE Distributor_ID='"+DistributorId.getText()+"'");
if(!resultSet.isBeforeFirst()){
//popup and add data
}
else{
preparedStatement = connect.prepareStatement("INSERT INTO Purchase Values(curdate()"+",?)");
preparedStatement.setString(1, "Pur"+sale_id.toString());
preparedStatement.executeUpdate();
System.out.println("Yeah!");
System.out.println(model.getRowCount());
for (int i = 0; i < model.getRowCount(); i++) {
String drugid = (String)model.getValueAt(i,1);
Integer Quantity=Integer.parseInt((String)model.getValueAt(i,2));
preparedStatement = connect.prepareStatement("INSERT INTO Supplies Values("+Quantity.toString()+",?,?,?)");
preparedStatement.setString(1, "Pur"+sale_id.toString());
preparedStatement.setString(2,DistributorId.getText());
preparedStatement.setString(3,drugid);
preparedStatement.executeUpdate();
preparedStatement = connect.prepareStatement("UPDATE Drug1 SET Stock = Stock + "+Quantity.toString()+" WHERE Drug_ID=?");
preparedStatement.setString(1,drugid);
preparedStatement.executeUpdate();
}
}
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
}catch(Exception e){
System.out.println(e);
}
model.getValueAt(0,0);
//whatevers in the jtable update it to database
//transaction is done
}
public void addalltocart(){
String medname=DrugId.getText();
if(!medname.equals("")){
//perform query and store result in these arrays
Connection connect = null;
Statement statement = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/dbms?"+"user=root&password=pass");
statement = connect.createStatement();
resultSet = statement.executeQuery("SELECT * FROM Drug1 WHERE Drug_ID='"+medname+"'");
if(!resultSet.isBeforeFirst()){
//add warning no such data
}
else{
while(resultSet.next()){
model.addRow(new Object[]{resultSet.getString("Name"),resultSet.getString("Drug_ID"),Quantity.getText()});
}
}
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
}catch(Exception e){
System.out.println(e);
}
centre.repaint();
centre.revalidate();
}
}
public void searchmed(){
String name=SearchMed.getText();
//search medicines with name like this
//2d array of results
JFrame meds=new JFrame();
JLabel heading = new JLabel("Chemist DBMS");
heading.setFont(new Font("Verdana",1,40));
meds.add(heading,BorderLayout.NORTH);
DefaultTableModel model1 = new DefaultTableModel();
model1.addColumn("DrugId");
model1.addColumn("Name");
model1.addColumn("Stock");
model1.addColumn("Price");
model1.addColumn("Location");
model1.addColumn("Expiry");
JTable tablemeds=new JTable(model1);
Connection connect = null;
Statement statement = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//DefaultTableModel mod=(DefaultTableModel)table.getModel();
try{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/dbms?"+"user=root&password=pass");
statement = connect.createStatement();
resultSet = statement.executeQuery("SELECT * FROM Drug1 NATURAL JOIN Drug2 Where Drug1.Name LIKE \"%"+name+"%\"");
while(resultSet.next()){
model1.addRow(new Object[]{resultSet.getString("Drug_ID"),resultSet.getString("Name"),resultSet.getString("Stock"),resultSet.getString("MRP"),resultSet.getString("Location"),resultSet.getString("Drug_ID")});
}
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
}catch(Exception e){
System.out.println(e);
}
meds.add( new JScrollPane( tablemeds ), BorderLayout.CENTER );
meds.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
meds.setSize(new Dimension(400,200));//600
meds.setVisible(true);
centre.repaint();
centre.revalidate();
}
class reset implements ActionListener{
public void actionPerformed(ActionEvent e){
GuiQuery9.this.reset();
}
}
class find implements ActionListener{
public void actionPerformed(ActionEvent e){
GuiQuery9.this.searchmed();
}
}
class submit implements ActionListener{
public void actionPerformed(ActionEvent e){
GuiQuery9.this.runQuery();
}
}
class addtocart implements ActionListener{
public void actionPerformed(ActionEvent e){
GuiQuery9.this.addalltocart();
}
}
}