-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.java
262 lines (246 loc) · 6.31 KB
/
framework.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
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
//Framework program in Java using MySQL database
import java.sql.*;
import java.util.Scanner;
class Framework
{
Statement statement;
Connection connection;
Scanner scan = new Scanner(System.in);
String[] fieldNames;
String[] messages;
ResultSet result;
String query;
String menu;
String[] updatableFieldsIndex;
String errorMessage = "PLEASE ENTER A VALID OPTION.";
public void storeFieldNames() throws SQLException
{
query = "select * from my_table1";
result = statement.executeQuery(query);
ResultSetMetaData rsmd = result.getMetaData();
fieldNames = new String[rsmd.getColumnCount() - 1];
for(int index = 0; index < fieldNames.length; index++)
{
if(rsmd.getColumnName(index + 1).equals("Status") == false)
{
fieldNames[index] = rsmd.getColumnName(index + 1);
}
}
}
public void createRecord() throws SQLException
{
String fieldValues = "";
String fieldValue = "";
for(String fieldName : fieldNames)
{
System.out.print("Enter " + fieldName + ": ");
fieldValue = scan.nextLine();
fieldValues += "'" + fieldValue + "'" + ", ";
}
fieldValues += "'A'";
query = "insert into my_table1 values(" + fieldValues + ")";
int noOfRowsAffected = statement.executeUpdate(query);
if(noOfRowsAffected == 1)
{
System.out.println(messages[0]);
}
}
public void searchRecord() throws SQLException
{
String idToSearchRecord = getId();
boolean isRecordPresent = checkRecordPresentOrNot(idToSearchRecord);
if(isRecordPresent)
{
query = "select * from my_table1 where Status = 'A' and " + fieldNames[0] + " = '" + idToSearchRecord + "'";
result = statement.executeQuery(query);
printRecord(result);
}
else
{
System.out.println(messages[2]);
}
}
public void printAllRecords() throws SQLException
{
query = "select * from my_table1 where Status ='A'";
result = statement.executeQuery(query);
printRecord(result);
}
public void printRecord(ResultSet result) throws SQLException
{
while(result.next())
{
for(String fieldName : fieldNames)
{
System.out.println(fieldName + ": " + result.getString(fieldName));
}
System.out.println("-----------------------------------------------------");
}
}
public void deleteRecord() throws SQLException
{
String idToDeleteRecord = getId();
query = "update my_table1 set Status = 'D' where " + fieldNames[0] + " = '" + idToDeleteRecord + "'";
int noOfRowsAffected = statement.executeUpdate(query);
if(noOfRowsAffected == 0)
{
System.out.println(messages[2]);
}
else
{
System.out.println(messages[1]);
}
}
public void updateRecord() throws SQLException
{
String idToUpdateRecord = getId();
boolean isRecordPresent = checkRecordPresentOrNot(idToUpdateRecord);
if(isRecordPresent)
{
updatableFieldsIndex = getData("updatable_fields_index");
int counter = 1;
for(String fieldIndex : updatableFieldsIndex)
{
System.out.println(counter + ". Update " + fieldNames[Integer.parseInt(fieldIndex) - 1]);
counter ++;
}
System.out.print("Enter a number: ");
int choice = scan.nextInt();
scan.nextLine();
if(choice > 0 && choice < counter)
{
String fieldName = fieldNames[Integer.parseInt(updatableFieldsIndex[choice - 1]) - 1];
System.out.print("Enter new " + fieldName + ": ");
String fieldValue = scan.nextLine();
String query1 = "update my_table1 set " + fieldName + " = '" + fieldValue + "' where " + fieldNames[0] + " = '" + idToUpdateRecord + "'";
int noOfRowsAffected = statement.executeUpdate(query1);
if(noOfRowsAffected == 1)
{
System.out.println(fieldName + " is updated.");
}
else
{
System.out.println("Error in updating " + fieldName + ".");
}
}
else
{
System.out.println(errorMessage);
}
}
else
{
System.out.println(messages[2]);
}
}
public String getId()
{
System.out.print("Enter " + fieldNames[0] + ": ");
String fieldValue = scan.nextLine();
return fieldValue;
}
public boolean checkRecordPresentOrNot(String Id) throws SQLException
{
boolean isRecordPresent = false;
query = "select " + fieldNames[0] + " from my_table1 where Status = 'A' and " + fieldNames[0] + " = '" + Id + "'";
result = statement.executeQuery(query);
if(result.next())
{
isRecordPresent = true;
}
return isRecordPresent;
}
public String[] getData(String fileName) throws SQLException
{
query = "select * from Config where FileName = '" + fileName + "'";
result = statement.executeQuery(query);
String tempData = "";
while(result.next())
{
tempData = result.getString("FileContent");
}
return tempData.split(", ");
}
public Framework()
{
String url = "jdbc:mysql://165.22.14.77/dbYashwanth?user=Yashwanth&password=Yashwanth";
try
{
connection = DriverManager.getConnection(url);
System.out.println("Connected Successfuly.");
statement = connection.createStatement();
storeFieldNames();
storeMessages();
storeMenu();
}
catch(SQLException error)
{
System.out.println(error.getMessage());
}
}
public void storeMessages() throws SQLException
{
messages = getData("messages");
}
public void storeMenu() throws SQLException
{
String query = "select * from Config where FileName = 'menu'";
result = statement.executeQuery(query);
while(result.next())
{
menu = result.getString("FileContent");
}
}
public void printMenu() throws SQLException
{
while(true)
{
System.out.println(menu);
System.out.print("Enter a number: ");
String choice = scan.next();
scan.nextLine();
switch(choice)
{
case "1": createRecord();
break;
case "2": searchRecord();
break;
case "3": printAllRecords();
break;
case "4": updateRecord();
break;
case "5": deleteRecord();
break;
case "6": System.out.println("Are you sure? ");
System.out.print("Press Y or N: ");
String exitChoice = scan.next();
if(exitChoice.toUpperCase().equals("Y"))
{
scan.close();
connection.close();
System.exit(0);
}
else if(exitChoice.toUpperCase().equals("N"))
{
continue;
}
default: System.out.println(errorMessage);
}
}
}
}
class MainClass
{
public static void main(String args[])
{
Framework framework = new Framework();
try
{
framework.printMenu();
}
catch(SQLException error)
{
System.out.println(error.getMessage());
}
}
}