-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableFunctionChat.java
More file actions
145 lines (130 loc) · 6.13 KB
/
Copy pathTableFunctionChat.java
File metadata and controls
145 lines (130 loc) · 6.13 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
package sqliteExample;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import sqliteExample.TableAttributeServer;
public class TableFunctionChat extends TableAttributeServer {
private static Connection mConnection = null;
private static Statement mStatement = null;
public static boolean create(String dbName) {
try {
mConnection = DriverManager.getConnection("jdbc:sqlite:" + dbName + ".db");
System.out.println("Opened database successfully");
mStatement = mConnection.createStatement();
String sql = "CREATE TABLE if not exists " + CHAT_TABLE + "(" + CHAT_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + CHAT_FROM_WHO + " TEXT NOT NULL, "
+ CHAT_TO_WHO + " TEXT NOT NULL, " + CHAT_CONTENT + " TEXT, "
+ CHAT_TIME + " INTEGER NOT NULL, " + CHAT_IS_GROUP + " BOOLEAN NOT NULL, "+ CHAT_POST_ID + " INTEGER, "
+ CHAT_READ_STATUS + " TEXT " + ")";
mStatement.executeUpdate(sql);
mStatement.close();
mConnection.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
return (false);
}
System.out.println("Table created successfully");
return true;
}
public static boolean insert(JSONArray rawData) {
ArrayList<String> chatFromWho = new ArrayList<String>();
ArrayList<String> chatToWho = new ArrayList<String>();
ArrayList<String> chatContent = new ArrayList<String>();
ArrayList<Long> chatTime = new ArrayList<Long>();
ArrayList<Boolean> chatIsGroup = new ArrayList<Boolean>();
ArrayList<Long> chatPostID = new ArrayList<Long>();
ArrayList<String> chatReadStatus = new ArrayList<String>();
String value = "";
for (int i = 0; i < rawData.size(); i++) {
JSONObject jobj = (JSONObject) rawData.get(i);
chatFromWho.add(jobj.get(CHAT_FROM_WHO).toString());
chatToWho.add(jobj.get(CHAT_TO_WHO).toString());
chatContent.add(jobj.get(CHAT_CONTENT).toString());
chatTime.add((Long)jobj.get(CHAT_TIME));
chatIsGroup.add((Boolean)jobj.get(CHAT_IS_GROUP));
chatPostID.add((Long)jobj.get(CHAT_POST_ID));
chatReadStatus.add(jobj.get(CHAT_READ_STATUS).toString());
value = value + "('" + chatFromWho.get(i) + "', '" + chatToWho.get(i) + "', '"
+ chatContent.get(i) + "', '" + chatTime.get(i) + "', '" + chatIsGroup.get(i) + "', '"+ chatPostID.get(i) + "', '"
+ chatReadStatus.get(i) + "')";
if (i != rawData.size() - 1) {
value = value + ", ";
} else {
value = value + ";";
}
}
System.out.println("value is : " + value);
try {
mConnection = DriverManager.getConnection("jdbc:sqlite:" + DB_NAME + ".db");
mConnection.setAutoCommit(false);
System.out.println("Opened database successfully");
mStatement = mConnection.createStatement();
String sql = "INSERT INTO " + CHAT_TABLE + " (" + CHAT_FROM_WHO + "," + CHAT_TO_WHO + ","
+ CHAT_CONTENT + "," + CHAT_TIME + "," + CHAT_IS_GROUP + ","+ CHAT_POST_ID + ","
+ CHAT_READ_STATUS + ") " + "VALUES " + value;
mStatement.executeUpdate(sql);
mStatement.close();
mConnection.commit();
mConnection.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Records created successfully");
return true;
}
public static boolean update(int userChatID, JSONObject rawData) throws FileNotFoundException, IOException,
ParseException {
String setValue = "";
for (@SuppressWarnings("rawtypes")
Iterator iterator = rawData.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
setValue = setValue + key + " = " + "'" + rawData.get(key) + "',";
}
setValue = setValue.substring(0, setValue.length() - 1);
System.out.println("setValue is : " + setValue);
try {
mConnection = DriverManager.getConnection("jdbc:sqlite:" + DB_NAME + ".db");
mConnection.setAutoCommit(false);
System.out.println("update Opened database successfully");
mStatement = mConnection.createStatement();
String sql = "UPDATE " + CHAT_TABLE + " set " + setValue + " where " + CHAT_ID + " = "
+ userChatID + ";";
System.out.println("test : " + sql);
mStatement.executeUpdate(sql);
mConnection.commit();
mStatement.close();
mConnection.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Operation done successfully");
return true;
}
public static boolean delete(int userChatID) {
try {
mConnection = DriverManager.getConnection("jdbc:sqlite:" + DB_NAME + ".db");
mConnection.setAutoCommit(false);
System.out.println("Opened database successfully");
mStatement = mConnection.createStatement();
String sql = "DELETE from " + CHAT_TABLE + " where " + CHAT_ID + "= " + userChatID + ";";
mStatement.executeUpdate(sql);
mConnection.commit();
mStatement.close();
mConnection.close();
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
return true;
}
}