public class DataProcessing {
private static boolean connectToDB=false;
static Hashtable<String,User> users;
static {
users=new Hashtable<String,User>();
users.put("hjy", new Operator("hjy","111","operator"));
users.put("cr", new Browser("cr","000","browser"));
users.put("myk", new Administrator("myk","250","administrator"));
Init();
}
public static void Init() {
//connect to database
//update database connection status
if(Math.random()>0.2) connectToDB=true;
else connectToDB=false;
}
public static User searchUser(String name) throws SQLException{
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name)) {
return users.get(name);
}
return null;
}
public static User search(String name,String password) throws SQLException{
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name)) {
User temp=users.get(name);
if(temp.getPassword().equals(password))
return temp;
}
return null;
}
public static Enumeration<User> getAllUser() throws SQLException{
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
Enumeration<User> e=users.elements();
return e;
}
public static boolean update(String name,String password,String role) throws SQLException{
User user;
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name)) {
if(role.equalsIgnoreCase("administrator"))
user=new Administrator(name,password,role);
else if(role.equalsIgnoreCase("operator"))
user=new Operator(name,password,role);
else
user=new Browser(name,password,role);
users.put(name, user);
return true;
}
else return false;
}
public static boolean insert(String name,String password,String role) throws SQLException{
User user;
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name))
return false;
else {
if(role.equalsIgnoreCase("administrator"))
user=new Administrator(name,password,role);
else if(role.equalsIgnoreCase("operator"))
user=new Operator(name,password,role);
else
user=new Browser(name,password,role);
users.put(name, user);
return true;
}
}
public static boolean delete(String name) throws SQLException{
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name)) {
users.remove(name);
return true;
}
else return false;
}
public void disconnectFromDB() {
if(connectToDB) {
//close Statement and Connection
try {
if(Math.random()>0.5) throw new SQLException("Error in disconnecting DB");
}
catch(SQLException sqlException) {
sqlException.printStackTrace();
}finally {
connectToDB=false;
}
}
}
}
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