-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbyte_array_to_blob
More file actions
45 lines (40 loc) · 1.73 KB
/
byte_array_to_blob
File metadata and controls
45 lines (40 loc) · 1.73 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
// table definition
CREATE TABLE `messages` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`from` int(11) NOT NULL,
`to` int(11) NOT NULL,
`content` varbinary(5000) NOT NULL,
PRIMARY KEY (`id`),
KEY `to_idx` (`to`),
KEY `from` (`from`),
CONSTRAINT `from` FOREIGN KEY (`from`) REFERENCES `users` (`id`),
CONSTRAINT `to` FOREIGN KEY (`to`) REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
//reading data from database
String sql = "SELECT content FROM secret_messages.messages where `to`=?";
Connection con = ds.getCon();
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, 4);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
//String content=rs.getString(1);
byte[] content = rs.getBytes("content");// varbinary or blob columns
try{
originalContent = RSA.decrypt(content , privateKey);
response.getOutputStream().print("<br/>");
response.getOutputStream().print(new String(originalContent));
}catch(Exception e){
response.getOutputStream().print("Not possile to decript:"+new String(content));
e.printStackTrace();
}
}
//writting data into database
ds = new DataSourceMySQL();
sql = "insert into messages (`from`,`to`,content) values(?,?,?)";
con = ds.getCon();
ps = con.prepareStatement(sql);
ps.setLong(1, from);
ps.setLong(2, to);
//ps.setString(3, content);//byte[] variable
ps.setBytes(3, messageCyphered);//VARBINARY OR BLOB ON MySQL
boolean result = ps.execute();