-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaMySQL_CLI.java
More file actions
252 lines (157 loc) · 5.96 KB
/
JavaMySQL_CLI.java
File metadata and controls
252 lines (157 loc) · 5.96 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
import java.util.Scanner;
public class JavaMySQL_CLI {
public static void main(String[] args) throws TableNotFoundException, TableNotImportedException {
String[] serverQuestions = new String[] {"Enter Server Address: \njdbc:mysql://", "Enter Port Number: ", "Enter Username: ",
"Enter Name For This Connection: ", "Enter (Exact) Name of Database: ", "**For security, this is not stored **\nEnter Server Password: "};
Server serv = new Server();
Database database = new Database();
boolean gettingServerInfo = true;
boolean readyForNext = true;
int ServerQuestionCounter = -1;
String address = "";
String username = "";
int port = 0;
String password = "";
String connName = "";
String visual_address = "";
String connectionName = "";
String databaseName = "";
boolean mainMenu = false;
boolean newSelection = false;
Scanner input = new Scanner(System.in);
while(gettingServerInfo) {
readyForNext = true;
ServerQuestionCounter = ServerQuestionCounter +1;
while(readyForNext) {
readyForNext = false;
/* Address */
if(ServerQuestionCounter == 0) {
System.out.print(serverQuestions[0]);
if(input.hasNext()) {
address = input.nextLine();
serv.setAddress(address);
visual_address = "jdbc:mysql://" + address;
System.out.print("\n\n\n");
}
}
/* Port */
else if (ServerQuestionCounter == 1) {
System.out.println(serverQuestions[1]);
System.out.print(address + "/");
if(input.hasNext()) {
port = input.nextInt();
serv.setPort(port);
System.out.print("\n\n\n");
}
}
/* Username */
else if (ServerQuestionCounter == 2) {
System.out.print(serverQuestions[2]);
if(input.hasNext()) {
username = input.next();
serv.setUsername(username);
System.out.print("\n\n\n");
}
}
/* Connection Name */
else if (ServerQuestionCounter == 3) {
Scanner connNameQuery = new Scanner(System.in);
System.out.print(serverQuestions[3]);
connName = connNameQuery.next();
readyForNext = false;
}
/* Database Name */
else if (ServerQuestionCounter == 4) {
Scanner dbNameScanner = new Scanner(System.in);
System.out.print(serverQuestions[4]);
if(dbNameScanner.hasNext()) {
databaseName = dbNameScanner.nextLine();
database.setName(databaseName);
System.out.print("\n\n\n");
}
}
/* Password */
else if (ServerQuestionCounter == 5) {
System.out.print(serverQuestions[5]);
Scanner passScanner = new Scanner(System.in);
while(passScanner.hasNext()) {
System.out.print("\n\n\n");
password = passScanner.next();
serv.updateFullUrl();
database.setServer(serv);
database.setName("Connection");
database.setDbName(databaseName);
database.formConnection(password);
mainMenu = true;
newSelection = true;
gettingServerInfo = false;
break;
}
}
}
}
while (mainMenu) {
if (newSelection = true) {
newSelection = false;
Scanner tableSelect = new Scanner(System.in);
System.out.println("\n\n\n\n\n1) Load and List Table Names \n2) List Loaded Tables\n3) Import Table \n4) List Imported Tables \n5) Display Imported Table in Table Format \n6) List Columns Within Imported Table\n7) List Values Within Column \n8) Detatch Connection");
System.out.print("Enter Selection: ");
int selection = tableSelect.nextInt();
if (selection == 1) {
database.findTables();
database.listTables();
newSelection = true;
} else if (selection == 2) {
database.listTables();
newSelection = true;
} else if (selection == 3) {
String tablename;
Scanner tableScanner = new Scanner(System.in);
System.out.print("Enter name of table to import: ");
tablename = tableScanner.next();
try {
database.importTable(tablename);
} catch (TableNotImportedException e) {System.out.println(e);}
newSelection = true;
} else if (selection == 4) {
database.listImportedTables();
newSelection = true;
} else if (selection == 5) {
String tablename;
Scanner tableScanner = new Scanner(System.in);
System.out.print("Enter table to display: ");
tablename = tableScanner.next();
try {
database.showTable(tablename);
} catch (NullPointerException e)
{ System.out.println(e.getMessage());
throw new TableNotFoundException(tablename);
}
newSelection = true;
} else if (selection == 6) {
String tableName;
Scanner tableScanner = new Scanner(System.in);
System.out.print("Enter the table that the column resides: ");
tableName = tableScanner.next();
database.listColumnNames(tableName);
newSelection = true;
} else if (selection == 7) {
String tableName;
Scanner tableScanner = new Scanner(System.in);
System.out.print("Enter the table that the column resides: ");
tableName = tableScanner.next();
String columnName;
Scanner columnScanner = new Scanner(System.in);
System.out.print("Enter the name of the column: ");
columnName = columnScanner.next();
database.listColumnValues(tableName, columnName);
newSelection = true;
} else if (selection == 8) {
System.out.println("Have a nice day.");
database.detachConnection();
System.exit(0);
}
}
}
}
}