This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB.java
247 lines (193 loc) · 6.1 KB
/
DB.java
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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// Notice, do not import com.mysql.cj.jdbc.*
// or you will have problems!
public class DB {
private String url = "jdbc:mysql://localhost:3306/test_db";
private Connection conn = null;
DB() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "1234");
// Do something with the Connection
System.out.println("DONE");
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
void startConn() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, "root", "1234");
// Do something with the Connection
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
// insert action
boolean insertNewNoteDB(String data, String title) {
String sql = "INSERT INTO test_db.notes_test (note_title, note_text) VALUES ('" + title + "' ,'" + data + "')";
System.out.println(title + "inserting");
dealQuery(sql);
return true;
}
// Gets the current counter value
int getNoRows() {
dealConn();
int rows = 0;
// SELECT CustomerName, City FROM Customers;
String sql = "SELECT note_id FROM test_db.notes_test order by note_id desc limit 1;";
try {
Statement statement = conn.prepareStatement(sql);
ResultSet outputSet = statement.executeQuery(sql);
while (outputSet.next()) {
rows = Integer.valueOf(outputSet.getString(1));
}
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (rows == 0) {
rows = 1;
}
rows++;
System.out.println(rows + " Counter");
return rows;
}
// Deals the SQL query
void dealQuery(String sql) {
dealConn();
try {
Statement statement = conn.prepareStatement(sql);
statement.execute(sql);
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// Checks if the connection is still available if not then opens a new conection
void dealConn() {
startConn();
}
void loadDataFromDB() {
dealConn();
String sql = "SELECT * FROM test_db.notes_test WHERE intrash = 0;";
Notes.titleHMap.clear();
Notes.textHMap.clear();
try {
Statement statement = conn.prepareStatement(sql);
ResultSet outputSet = statement.executeQuery(sql);
while (outputSet.next()) {
int id = outputSet.getInt("note_id");
String title = outputSet.getString("note_title"); //
String text = outputSet.getString("note_text");
Notes.titleHMap.put(id, title);
Notes.textHMap.put(id, text);
}
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
loadDELDataFromDB();
Notes.updateListModal();
}
void loadDELDataFromDB() {
dealConn();
String sql = "SELECT * FROM test_db.notes_test WHERE intrash >= 1;";
Notes.titleSHMapDEL.clear();
Notes.textSHMapDEL.clear();
try {
Statement statement = conn.prepareStatement(sql);
ResultSet outputSet = statement.executeQuery(sql);
while (outputSet.next()) {
int id = outputSet.getInt("note_id");
String title = outputSet.getString("note_title"); //
String text = outputSet.getString("note_text");
Notes.titleSHMapDEL.put(id, title);
Notes.textSHMapDEL.put(id, text);
}
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
boolean updateTextAndTitle(int id, String title, String text) {
boolean insert = false;
dealConn();
String sql = "UPDATE test_db.notes_test SET note_text = '" + text + " ' WHERE note_id = " + id;
String sql1 = "UPDATE test_db.notes_test SET note_title = '" + title + " ' WHERE note_id = " + id;
try {
Statement statement = conn.prepareStatement(sql);
int s1 = statement.executeUpdate(sql);// execute(sql);
statement.close();
Statement statement1 = conn.prepareStatement(sql1);
int s2 = statement1.executeUpdate(sql1);
statement1.close();
System.out.println(s1 + " " + s2);
Notes.titleHMap.replace(id, title);
Notes.textHMap.replace(id, text);
} catch (SQLException e) {
e.printStackTrace();
}
Notes.updateListModal();
return insert;
}
boolean deleteRowWithID(int id) {
boolean insert = false;
dealConn();
String sql = "UPDATE test_db.notes_test SET intrash = 1 where note_id =" + id;
// DELETE FROM `table_name` [WHERE condition];
try {
Statement statement = conn.prepareStatement(sql);
statement.executeUpdate(sql);
statement.close();
Notes.titleHMap.remove(id);
Notes.textHMap.remove(id);
} catch (SQLException e) {
e.printStackTrace();
}
Notes.updateListModal();
return insert;
}
void updateRestore(int id, int action, String title, String text) {
dealConn();
if (action == 1) {
String sql = "UPDATE test_db.notes_test SET intrash = 0 WHERE note_id = " + id;
try {
Statement statement = conn.prepareStatement(sql);
statement.executeUpdate(sql);
Notes.titleHMap.put(id, title);
Notes.textHMap.put(id, text);
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
} else if (action >= 2) {
String sql = "DELETE FROM test_db.notes_test WHERE note_id = " + id;
Notes.titleSHMapDEL.remove(id);
Notes.textSHMapDEL.remove(id);
try {
Statement statement = conn.prepareStatement(sql);
statement.executeUpdate(sql);
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
Notes.updateListModal();
}
}