-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManageData.java
195 lines (173 loc) · 7.49 KB
/
ManageData.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
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDate;
public class ManageData {
public final static String HO_QUEUE = "ho_queue";
public final static String HO_DBNAME = "HO";
public static void createDb(String dbName){
try{
java.sql.Connection con= DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=");
Statement stmt=con.createStatement();
Boolean result = stmt.execute("CREATE DATABASE if not exists "+dbName);
con.close();
}catch( Exception e){
System.out.println("error in creating data base "+ dbName);
System.out.println(e);
}
}
public static void createProductTable(String dbName){
try{
java.sql.Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName,"root","");
Statement stmt=con.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS product (" +
"id VARCHAR(100)," +
"product VARCHAR(50)," +
"qty INT," +
"cost FLOAT," +
"amt FLOAT," +
"tax FLOAT," +
"total FLOAT," +
"region VARCHAR(50)" +
")";
stmt.executeUpdate(sql);
con.close();
}catch(Exception e){
System.out.println("error in creating the product table in database "+ dbName);
System.out.println(e);
}
}
public static void sendToDB(Product p, String dbName){
try{
java.sql.Connection con= DriverManager.getConnection(
"jdbc:mysql://localhost:3306/"+dbName,"root","");
String sql = "replace into product (id, product, qty, cost, amt, tax, total, region) values (?, ?, ?, ?, ?, ?, ?, ?);";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, p.id);
stmt.setString(2, p.product);
stmt.setInt(3, p.qty);
stmt.setFloat(4, p.cost);
stmt.setFloat(5, p.amt);
stmt.setFloat(6, p.tax);
stmt.setFloat(7, p.total);
stmt.setString(8, p.region);
stmt.executeUpdate();
con.close();
}catch(Exception e){
System.out.println("failed to send the product: " + p.product );
System.out.println(e);
}
}
public static String[][] getAllProducts(String dbName){
String[][] products = new String[100][9];
int i=0;
try {
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, "root", "");
Statement stmt = con.createStatement();
String sql = "SELECT * FROM product;";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
products[i][0] = rs.getString(1);
products[i][1] = rs.getString(2);
products[i][2] = Integer.toString(rs.getInt(3));
products[i][3] = Float.toString(rs.getFloat(4));
products[i][4] = Float.toString(rs.getFloat(5));
products[i][5] = Float.toString(rs.getFloat(6));
products[i][6] = Float.toString(rs.getFloat(7));
products[i][7] = rs.getString(8);
i++;
}
}catch (Exception e){
System.out.println("Error in retrieving data from "+ dbName );
}
String[][] products1 = new String[i][9];
for(int j=0 ; j<i; j++){
products1[j][0] = products[j][0];
products1[j][1] = products[j][1];
products1[j][2] = products[j][2];
products1[j][3] = products[j][3];
products1[j][4] = products[j][4];
products1[j][5] = products[j][5];
products1[j][6] = products[j][6];
products1[j][7] = products[j][7];
}
return products1;
}
public static void sendToHO(Product p) {
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(HO_QUEUE, false, false, false, null);
String msg = "ADD"+p.toString();
channel.basicPublish("", HO_QUEUE, null, msg.getBytes());
}catch (Exception e){
System.out.println("error sending data to HO ");
}
}
public static void sendQueryToHO(String sql) {
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(HO_QUEUE, false, false, false, null);
String msg = sql.toString();
channel.basicPublish("", HO_QUEUE, null, msg.getBytes());
}catch (Exception e){
System.out.println("error sending data to HO ");
}
}
public static void sendOldDataToHO(String dbName) {
try {
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, "root", "root");
Statement stmt = con.createStatement();
String sql = "SELECT * FROM product;";
ResultSet product = stmt.executeQuery(sql);
while (product.next()){
String id = product.getString(1);
String productname = product.getString(2);
float qty = product.getInt(3);
float cost = product.getFloat(4);
float amt = product.getFloat(5);
float tax = product.getFloat(6);
float total = product.getFloat( 7);
String region = product.getString(8);
Product p = new Product(id,productname, (int) qty, cost, amt, tax, total, region);
sendToHO(p);
}
}catch (Exception e){
}
}
public static void execQuery(String sql,String dbName){
try{
java.sql.Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName,"root","");
Statement stmt=con.createStatement();
stmt.executeUpdate(sql);
con.close();
}catch(Exception e){
System.out.println("error in executing query : "+ sql);
}
}
public static void removeFromBD(String id, String dbName){
String sql = "DELETE FROM product WHERE id = '"+id+"';";
execQuery(sql,dbName);
}
public static void removeFromHO(String id){
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(HO_QUEUE, false, false, false, null);
channel.basicPublish("", HO_QUEUE, null, ("DEL"+id).getBytes());
}catch (Exception e){
System.out.println("error sending data to HO ");
}
}
}