-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseManager.java
More file actions
304 lines (257 loc) · 9.92 KB
/
DatabaseManager.java
File metadata and controls
304 lines (257 loc) · 9.92 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// package databasepkg;
import java.io.ByteArrayInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DatabaseManager
{
// The JDBC Connector Class.
private static final String dbClassName = "com.mysql.jdbc.Driver";
// Connection string. mypassDB is the database the program
// is connecting to. You can include user and password after this
// by adding (say) ?user=paulr&password=paulr. Not recommended!
private final String CONNECTION =
"jdbc:mysql://id-passdb.cyexrsggmza6.us-west-2.rds.amazonaws.com/mypassDB";
private Connection c;
private Statement st;
public void connection() throws ClassNotFoundException, SQLException {
System.out.println(dbClassName);
// Class.forName(xxx) loads the jdbc classes and
// creates a drivermanager class factory
Class.forName(dbClassName);
// Properties for user and password.
Properties p = new Properties();
p.put("user","hschang");
p.put("password","idon'tknow");
// Now try to connect
System.out.println("connecting to the database..");
c = DriverManager.getConnection(CONNECTION,p);
System.out.println("Database connected!");
}
public void end() throws SQLException{
// close statement
if(st!=null)
st.close();
// close the database connection
if(c!=null)
c.close();
}
/*
* Check if the user is valid
*/
public int loginCheck(String username, String password) throws SQLException {
String query = "SELECT * FROM Users WHERE username='"+username+"' AND password='"+password+"'";
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
return rs.next()? rs.getInt("ID") : 0;
}
/*
* insert data into table Credentials
*/
private boolean insertData(int id, String tag, byte[] data) throws SQLException {
String query = "INSERT INTO Credentials (tag, data, userid)"
+ " values (?, ?, ?)";
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = c.prepareStatement(query);
preparedStmt.setString (1, tag);
preparedStmt.setBinaryStream(2, byteArrayInputStream);
preparedStmt.setInt (3, id);
System.out.println("data inserting..");
// execute the preparedstatement
return preparedStmt.execute();
}
/*
* edit data in table Credentials by specifying correct id and tag
*/
// action:?
public boolean editData(int id, String tag, byte[] data) throws SQLException{
// check tag existence
String query = "SELECT * FROM Credentials WHERE userid="+id+" AND tag='"+tag+"'";
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
if (!rs.next()) {
System.err.println("The tag not exist!");
return false;
}
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
// create the java mysql update preparedstatement
query = "UPDATE Credentials SET data = ? WHERE userid = ? AND tag= ? ";
PreparedStatement preparedStmt = c.prepareStatement(query);
preparedStmt.setBinaryStream(1, byteArrayInputStream);
preparedStmt.setInt (2, id);
preparedStmt.setString(3, tag);
// execute the java preparedstatement
preparedStmt.executeUpdate();
System.out.println("data edited");
return true;
}
/*
* register account for the app
*/
// action:3
public int register(String username, String password) throws SQLException {
// check username existence
String query = "SELECT * FROM Users WHERE username='"+username+"'";
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
if (rs.next()) {
System.err.println("The username exists.");
return 0;
}
// the mysql insert statement
query = "INSERT INTO Users (username, password)"
+ " values (?, ?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = c.prepareStatement(query);
preparedStmt.setString (1, username);
preparedStmt.setString (2, password);
// execute the preparedstatement
preparedStmt.execute();
System.out.println("User registered.");
query = "SELECT ID FROM Users WHERE username='"+username+"'";
st = c.createStatement();
rs = st.executeQuery(query);
// return id
//System.out.println("ID= "+ID);
return rs.next()? rs.getInt("ID"): 0;
}
/*
* store data in table Credentials by specifying correct id
*/
// action:4
public boolean storeData(int id, String tag, byte[] data) throws SQLException {
// check tag existence
String query = "SELECT * FROM Credentials WHERE userid="+id+" AND tag='"+tag+"'";
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
if (rs.next()) {
System.err.println("The tag already exist! data not stored");
return false;
}
insertData(id, tag, data);
return true;
}
/*
* retrieve data in table Credentials if having correct id and tag
*/
// action:5
public byte[] retrieve(int id, String tag) throws SQLException {
String query = "SELECT * FROM Credentials WHERE userid="+id+" AND tag='"+tag+"'";
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
if(rs.next()){
byte[] data = rs.getBytes("data");
System.out.println("length of data retrieved from database = " + data.length);
return data;
} else {
byte[] data = {0};
System.out.println("length of data when retrieve from database failed = " + data.length);
return data;
}
}
//**** for testing purpose ****
/*
* given id, check the row data in table Users
*/
private void selectUsers(int id) throws SQLException{
// our SQL SELECT query.
// if you only need a few columns, specify them by name instead of using "*"
String query = "SELECT * FROM Users WHERE ID="+id;
// create the java statement
st = c.createStatement();
// execute the query, and get a java resultset
ResultSet rs = st.executeQuery(query);
// iterate through the java resultset
while (rs.next())
{
// int id = rs.getInt("ID");
String username = rs.getString("username");
String password = rs.getString("password");
// print the results
System.out.format("%s, %s, %s\n", id, username, password);
}
}
/*
* given username or password, check the row data in table Users
*/
private void selectUsers(String s) throws SQLException {
String query = "SELECT * FROM Users WHERE username='"+s+"'' OR password='"+s+"'";
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
int id = rs.getInt("ID");
String username = rs.getString("username");
String password = rs.getString("password");
System.out.format("%s, %s, %s\n", id, username, password);
}
}
/*
* given id, check the row data in table Credentials
*/
private void selectCredential(int id) throws SQLException {
String query = "SELECT * FROM Credentials WHERE userid="+id;
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
// int id = rs.getInt("ID");
String tag = rs.getString("tag");
String data = rs.getString("data");
System.out.format("%s, %s, %s\n", id, tag, data);
}
}
/*
* given username or password, check the row data in table Credentials
*/
private void selectCredential(String s) throws SQLException {
String query = "SELECT * FROM Users WHERE tag='"+s+"'' OR data='"+s+"'";
st = c.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
int id = rs.getInt("ID");
String tag = rs.getString("tag");
String data = rs.getString("data");
System.out.format("%s, %s, %s\n", id, tag, data);
}
}
// Constructor for testing purpose
// public DatabaseManager() {
// try {
// connection();
// // put action HERE!
// // System.out.println(loginCheck("Lars", "Monsen"));
// // selectUsers(1);
// // insertData(1, "Facebook", "qwenjklsvnklsda");
// // editData(1, "Facebook", "I have a pen. I have an apple.");
// // register("Sue", "passsss");
// // storeData(1, "Facebook", "I don't care");// should export error
// // storeData(1, "Facebook2", "I do care");// store the data
// // storeData(loginCheck(Sue passsss), "Facebook", "Sue 234325");// store the data
// // System.out.println(retrieve(1, "Facebook"));
// // System.out.println(retrieve(1, "Facebook3"));//error
// // System.out.println(retrieve(2, "Facebook"));
// end();
// }
// catch (ClassNotFoundException e) {
// System.err.println("Got an exception! ClassNotFoundException");
// System.err.println(e.getMessage());
// }
// catch (SQLException e) {
// System.err.println("Got an exception! SQLException");
// System.err.println(e.getMessage());
// }
// }
// public static void main(String[] args)
// {
// new DatabaseManager();
// }
}