-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropAllTables.java
92 lines (80 loc) · 3.26 KB
/
DropAllTables.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
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;
public class DropAllTables {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
// Load the properties file
input = new FileInputStream("config.properties");
prop.load(input);
// Get the property values
String url = prop.getProperty("db.url");
String user = prop.getProperty("db.user");
String password = prop.getProperty("db.password");
String filePath = prop.getProperty("queries.delete_tables.path");
// Create a connection to the database
Connection conn = DriverManager.getConnection(url, user, password);
// Read file line by line
BufferedReader reader = new BufferedReader(new FileReader(filePath));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
// Remove single line comments
line = line.split("--")[0].split("#")[0];
sb.append(line);
}
reader.close();
// Split file content into separate queries
String[] queries = sb.toString().split(";");
// Execute each query
Statement stmt = conn.createStatement();
for (String query : queries) {
if (!query.trim().isEmpty()) {
stmt.execute(query);
String tableName = extractTableName(query);
System.out.println("DELETED THE TABLE " + tableName + ".");
}
}
// Close the statement and the connection
stmt.close();
conn.close();
System.out.println("All queries executed successfully!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static String extractTableName(String query) {
// Splits the query by whitespace characters into parts
String[] parts = query.split("\\s+");
for (int i = 0; i < parts.length; i++) {
// Checks if the current part is "TABLE", considering "IF EXISTS" clause
if (parts[i].equalsIgnoreCase("TABLE")) {
// Checks for "IF EXISTS" clause which would add two more words before the actual table name
if (i + 1 < parts.length && parts[i + 1].equalsIgnoreCase("IF")) {
// The table name should follow the words "IF EXISTS"
return parts[i + 3];
} else {
// The table name should follow the word "TABLE"
return parts[i + 1];
}
}
}
return "UNKNOWN"; // Fallback if table name could not be extracted
}
}