-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDatabaseAndCSV.java
More file actions
338 lines (308 loc) · 8.62 KB
/
DatabaseAndCSV.java
File metadata and controls
338 lines (308 loc) · 8.62 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicLong;
//Tutorial to find files in the computer
//https://www.youtube.com/watch?v=XXkq73u9Uqg
public class DatabaseAndCSV
{
private static final File fil = new File("");
static String l;
static String password, username;
private static class Search implements Runnable
{
private final BlockingQueue<File> queued;
private final BlockingQueue<File> queuef;
private final AtomicLong ct;
private final int number;
public Search(final BlockingQueue<File> queued, final BlockingQueue<File> queuef, final AtomicLong ct,
final int number)
{
this.queued = queued;
this.queuef = queuef;
this.ct = ct;
this.number = number;
}
public void run()
{
try
{
File dir = queued.take();
while (dir != fil)
{
final File[] files = dir.listFiles();
if (files != null)
{
for (final File element : files)
{
if (element.isDirectory())
{
ct.incrementAndGet();
queued.put(element);
} else
{
queuef.put(element);
}
}
}
final long c = ct.decrementAndGet();
if (c == 0)
{
end();
}
dir = queued.take();
}
} catch (final InterruptedException ie)
{
}
}
private final void end()
{
try
{
queuef.put(fil);
} catch (final InterruptedException e)
{
}
for (int i = 0; i < number; i++) {
try {
queued.put(fil);
} catch (final InterruptedException e)
{
}
}
}
}
private static class CallableFileSearch implements Callable<File>
{
private final BlockingQueue<File> queued;
private final BlockingQueue<File> queuef;
private final String name;
private final int number;
public CallableFileSearch(final BlockingQueue<File> queued, final BlockingQueue<File> queuef,
final String name, final int number)
{
this.queued = queued;
this.queuef = queuef;
this.name = name;
this.number = number;
}
public File call() throws Exception
{
File file = queuef.take();
while (file != fil)
{
final String filename = file.getName().toLowerCase();
final String lf = name.toLowerCase();
if (filename.equalsIgnoreCase(name) || filename.startsWith(lf) || filename.endsWith(lf))
{
end();
return file;
}
file = queuef.take();
}
return null;
}
private final void end()
{
for (int i = 0; i < number; i++)
{
try {
queued.put(fil);
} catch (final InterruptedException e)
{
}
}
}
}
private final String filename;
private final File baseD;
private final int concurrency;
private final AtomicLong ct;
public DatabaseAndCSV(final String filename, final File baseD, final int concurrency)
{
this.filename = filename;
this.baseD = baseD;
this.concurrency = concurrency;
ct = new AtomicLong(0);
}
public File find()
{
final ExecutorService x = Executors.newFixedThreadPool(concurrency + 1);
final BlockingQueue<File> queued = new LinkedBlockingQueue<File>();
final BlockingQueue<File> queuef = new LinkedBlockingQueue<File>(10000);
for (int i = 0; i < concurrency; i++)
{
x.submit(new Search(queued, queuef, ct, concurrency));
}
ct.incrementAndGet();
queued.add(baseD);
final Future<File> c = x.submit(new CallableFileSearch(queued, queuef, filename, concurrency));
try
{
final File f = c.get();
return f;
} catch (final Exception e)
{
return null;
} finally
{
x.shutdownNow();
}
}
/*
* Import csv file to database
* Export database information to csv file
* Put csv file to temporary path
*/
public static void main(final String[] args)
{
final String filename = "6c27ff1c-cd10-480f-b730-688f6d2ae56b.csv";
final File baseDir = new File("C:/");
final DatabaseAndCSV ff = new DatabaseAndCSV(filename, baseDir, 6);
File f = ff.find();
System.out.println(f);
l = f.toString();
// The location of the csv file to import information to the database
String importfilename = l;
// The location to put the csv file after exporting the table from the database
// String exportfilename = "C:\\Users\\Isabel Ho\\Desktop\\schedule_table.csv";
File file = new File(importfilename);
Connection connectDatabase = null;
Statement insertTable = null;
Statement createTable;
FileInputStream input_info = null;
/*
* Get password and username information from textfile to connect to the database
*/
Properties properties = new Properties();
try {
FileInputStream stream = new FileInputStream("input.txt");
properties.load(stream);
username = properties.getProperty("user");
password = properties.getProperty("pwd");
} catch (IOException ex)
{
ex.printStackTrace();
} finally {
if (input_info != null)
{
try
{
input_info.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
try
{
BufferedReader buffer = new BufferedReader(new FileReader(file));
String line;
String zero = null, one = null, two = null, three = null, four = null;
try
{
String url = "jdbc:mysql://localhost:3306/schedule_table";
Class.forName("com.mysql.jdbc.Driver");
connectDatabase = DriverManager.getConnection(url, username, password);
insertTable = connectDatabase.createStatement();
} catch (Exception e)
{
e.printStackTrace();
}
line = buffer.readLine();
String[] array = line.split(";");
/*
* Create table in the database
*/
try
{
createTable = connectDatabase.createStatement();
String create = "create table schedule_table(" + array[0] + " varchar(255)," + array[1] + " int,"
+ array[2] + " varchar(255)," + array[3] + " varchar(255)," + array[4] + " varchar(255))";
zero = array[0];
one = array[1];
two = array[2];
three = array[3];
four = array[4];
createTable.executeUpdate(create);
System.out.println("The table was created sucessfully");
} catch (Exception e)
{
e.printStackTrace();
}
/*
* Insert information into the table
*/
while ((line = buffer.readLine()) != null)
{
array = line.split(";");
try
{
String insert = "INSERT INTO schedule_table " + "(" + zero + "," + one + "," + two + "," + three
+ "," + four + ") " + "VALUES ('" + array[0] + "','" + array[1] + "','" + array[2] + "'"
+ ",'" + array[3] + "','" + array[4] + "') ";
insertTable.execute(insert);
} catch (SQLException e)
{
e.printStackTrace();
}
System.out.println("The information was imported successfully");
}
buffer.close();
} catch (IOException e)
{
e.printStackTrace();
}
/*
* Create a temporary file
* Get temporary file path
*/
try
{
File temporary = File.createTempFile("temporary-file", ".csv");
System.out.println("Temporary file : " + temporary.getAbsolutePath());
String path1 = temporary.getAbsolutePath();
String temporaryPath = path1.substring(0, path1.lastIndexOf(File.separator));
System.out.println("Temporary file path : " + temporaryPath);
FileWriter writer = new FileWriter(temporary);
String select = "select * from schedule_table";
Statement writeTable = connectDatabase.createStatement();
ResultSet result = writeTable.executeQuery(select);
while (result.next()) {
writer.append(result.getString(1));
writer.append(',');
writer.append(result.getString(2));
writer.append(',');
writer.append(result.getString(3));
writer.append(',');
writer.append(result.getString(4));
writer.append(',');
writer.append(result.getString(5));
writer.append('\n');
}
writer.flush();
writer.close();
connectDatabase.close();
System.out.println("CSV File is created successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}