-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableFunctionPost.java
More file actions
140 lines (124 loc) · 5.83 KB
/
Copy pathTableFunctionPost.java
File metadata and controls
140 lines (124 loc) · 5.83 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
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 TableFunctionPost 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 " + POST_TABLE + "(" + POST_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + POST_FROM_WHO + " TEXT NOT NULL, " + POST_TO_WHO
+ " TEXT NOT NULL, " + POST_SUBJECT + " TEXT, " + POST_CONTENT + " TEXT, " + POST_TIME
+ " INTEGER NOT NULL, " + POST_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> postFromWho = new ArrayList<String>();
ArrayList<String> postToWho = new ArrayList<String>();
ArrayList<String> postSubject = new ArrayList<String>();
ArrayList<String> postContent = new ArrayList<String>();
ArrayList<Long> postTime = new ArrayList<Long>();
ArrayList<String> postReadStatus = new ArrayList<String>();
String value = "";
for (int i = 0; i < rawData.size(); i++) {
JSONObject jobj = (JSONObject) rawData.get(i);
postFromWho.add(jobj.get(POST_FROM_WHO).toString());
postToWho.add(jobj.get(POST_TO_WHO).toString());
postSubject.add(jobj.get(POST_SUBJECT).toString());
postContent.add(jobj.get(POST_CONTENT).toString());
postTime.add((Long)jobj.get(POST_TIME));
postReadStatus.add(jobj.get(POST_READ_STATUS).toString());
value = value + "('" + postFromWho.get(i) + "', '" + postToWho.get(i) + "', '" + postSubject.get(i)
+ "', '" + postContent.get(i) + "', '" + postTime.get(i) + "', '" + postReadStatus.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 " + POST_TABLE + " (" + POST_FROM_WHO + "," + POST_TO_WHO + "," + POST_SUBJECT
+ "," + POST_CONTENT + "," + POST_TIME + "," + POST_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 postID, 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 " + POST_TABLE + " set " + setValue + " where " + POST_ID + " = " + postID + ";";
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 postID) {
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 " + POST_TABLE + " where " + POST_ID + "= " + postID + ";";
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;
}
}