-
Notifications
You must be signed in to change notification settings - Fork 0
/
DBCommands.java
185 lines (161 loc) · 6.06 KB
/
DBCommands.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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class DBCommands {
public static void main(String[] args) {
createDatabase("college");
createTable("college", "students");
insert("college", "students", "Dillon Enge");
insert("college", "students", "Connor Enge");
insert("college", "students", "Halle Enge");
insert("college", "students", "Belinda Enge");
insert("college", "students", "Trevor Enge");
select("college", "students");
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author: Dillon Enge
*******************************************/
public static boolean createDatabase(String dbName) {
File database = new File(dbName);
database.mkdir();
log("college", "status", ("Database " + dbName + " created." + "\n" + "(CREATE DATABASE " + dbName + ")\n"));
return true;
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author: Dillon Enge
*******************************************/
public static boolean dropDatabase(String dbName) {
File database = new File(dbName);
database.delete();
log("college", "status", ("Database " + dbName + " dropped." + "\n" + "(DROP DATABASE " + dbName + ")\n"));
return true;
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author:Dillon Enge
*******************************************/
public static boolean createTable(String dbName, String tableName) {
File table = new File(dbName + "/" + tableName + ".txt");
try {
table.createNewFile();
log("college", "status",
("Table " + tableName + " created." + "\n" + "(CREATE TABLE " + tableName + ")\n"));
return true;
} catch (IOException e) {
log("college", "status", ("Table " + tableName + " could not be created.\n" + e.getMessage()));
return false;
}
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author: Dillon Enge
*******************************************/
public static boolean dropTable(String dbName, String tableName) {
File table = new File(dbName + "/" + tableName + ".txt");
table.delete();
log("college", "status", ("Table " + tableName + " dropped." + "\n" + "(DROP TABLE " + tableName + ")\n"));
return true;
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author: Dillon Enge
*******************************************/
public static boolean insert(String dbName, String tableName, String input) {
File table = new File(dbName + "/" + tableName + ".txt");
try {
FileWriter fw = new FileWriter(table, true);
fw.write(input + "\n");
fw.close();
log("college", "status", ("Row " + input + " inserted into " + dbName + "." + tableName + " table" + "\n"
+ "(INSERT \"" + input + "\" INTO " + dbName + "." + tableName + ")\n"));
return true;
} catch (IOException e) {
log("college", "status", ("String " + input + " could not be inserted.\n" + e.getMessage()));
return false;
}
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author: Dillon Enge
*******************************************/
public static boolean select(String dbName, String tableName) {
File table = new File(dbName + "/" + tableName + ".txt");
try {
Scanner tableScanner = new Scanner(table);
log("college", "status", "Rows selected for " + dbName + "." + tableName);
while (tableScanner.hasNextLine()) {
log("college", "status", tableScanner.nextLine());
}
tableScanner.close();
log("college", "status", "(SELECT " + "*" + " FROM " + dbName + "." + tableName + ")");
return true;
} catch (FileNotFoundException e) {
log("college", "error", "File " + tableName + " not found." + e.getMessage());
return false;
}
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author: Dillon Enge
*******************************************/
public static boolean selectWhere(String dbName, String tableName, String input) {
File table = new File(dbName + "/" + tableName + ".txt");
String line = "";
try {
Scanner tableScanner = new Scanner(table);
log("college", "status", "Rows selected for " + dbName + "." + tableName);
while (tableScanner.hasNextLine()) {
line = tableScanner.nextLine();
if (line.equals(input)) {
log("college", "status", "Row FOUND for " + dbName + "." + tableName);
break;
}
}
log("college", "status", "(SELECT " + input + " FROM " + dbName + "." + tableName);
tableScanner.close();
return true;
} catch (FileNotFoundException e) {
log("college", "error", "File " + tableName + " not found." + e.getMessage());
return false;
}
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author: Dillon Enge
Date:
*******************************************/
public static boolean delete(String dbName, String tableName) {
dropTable(dbName, tableName);
createTable(dbName, tableName);
return true;
}
/******************************************
CREATE DATABASE method - mETHOD THAT creates a database by creating
a file in a directory.
Author: Dillon Enge
Date:
*******************************************/
public static void log(String dbName, String logName, String input) {
File log = new File(dbName + "/" + logName + ".txt");
try {
FileWriter fw = new FileWriter(log, true);
fw.write(input + "\n");
fw.close();
} catch (IOException e) {
log("college", "error", e.toString());
e.printStackTrace();
}
}
}