Newer
Older
package db;
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.ArrayList;
import java.util.Arrays;
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
/**
* Connect to a sample database
*/
// static Connection conn = null;
/*
* public static void connect() {
*
* try { // db parameters String url = "jdbc:sqlite:DBPissirDomain.db"; create a
* connection to the database conn = DriverManager.getConnection(url);
*
* System.out.println("Connection to SQLite has been established.");
*
* System.out.println("OIOI"); } catch (SQLException e) {
* System.out.println(e.getMessage()); } finally { try { if (conn != null) {
* conn.close(); } } catch (SQLException ex) {
* System.out.println(ex.getMessage()); } } }
*/
public static ArrayList<Dominio> domainsGet() throws SQLException {
// connect();
/**
* Connect to a sample database
*/
// public static Connection conn = null;
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:DBPissirDomain.db";
// create a connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to SQLite has been established.");
/*
* String sql= "SELECT * FROM Utenti"; Statement statement
* =conn.createStatement(); ResultSet result =statement.executeQuery(sql); while
* (result.next()) { String name=result.getString("User");
*
* String desc=result.getString("Desc"); System.out.println(name+desc); }
*/
String sql = "SELECT * FROM Domini";
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(sql);
System.out.println("57");
ArrayList<Dominio> res = new ArrayList<Dominio>();
while (result.next()) {
System.out.println("60");
String dom = result.getString("Domain");
String desc = result.getString("Desc");
// String users=result.getString("Users");
ArrayList<String> users = new ArrayList<String>(Arrays.asList(result.getString("Users").split(",")));
ArrayList<String> reso = new ArrayList<String>(Arrays.asList(result.getString("Resources").split(",")));
// String reso=result.getString("Resources");
String state = result.getString("State");
ArrayList<String> serv = new ArrayList<String>(Arrays.asList(result.getString("Services").split(",")));
// String serv=result.getString("Services");
Dominio d = new Dominio(dom, desc, users, reso, state, serv);
res.add(d);
System.out.println("74");
}
return res;
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
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
}
public static Dominio getDom(String dm) throws SQLException {
// connect();
// Statement statement =conn.createStatement();
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:DBPissirDomain.db";
// create a connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to SQLite has been established: getDom");
/*
* String sql= "SELECT * FROM Utenti"; Statement statement
* =conn.createStatement(); ResultSet result =statement.executeQuery(sql); while
* (result.next()) { String name=result.getString("User");
*
* String desc=result.getString("Desc"); System.out.println(name+desc); }
*/
PreparedStatement prepared = conn.prepareStatement("SELECT * FROM Domini WHERE Domain = ?");
prepared.setString(1, dm);
ResultSet result = prepared.executeQuery();
System.out.println("DBCISONO1");
// String sql= "SELECT * FROM Domini WHERE Domain = ?";
// ResultSet result =statement.executeQuery(sql);
// Dominio res=new Dominio();
while (result.next()) {
System.out.println("DBCISONO2");
String dom = result.getString("Domain");
String desc = result.getString("Desc");
// String users=result.getString("Users");
ArrayList<String> users = new ArrayList<String>(Arrays.asList(result.getString("Users").split(",")));
ArrayList<String> reso = new ArrayList<String>(Arrays.asList(result.getString("Resources").split(",")));
// String reso=result.getString("Resources");
String state = result.getString("State");
ArrayList<String> serv = new ArrayList<String>(Arrays.asList(result.getString("Services").split(",")));
// String serv=result.getString("Services");
Dominio d = new Dominio(dom, desc, users, reso, state, serv);
// res.add(d);
System.out.println(d.toString());
return d;
}
//return null;
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
return null;
}
public static void delDom(String dm) throws SQLException {
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:DBPissirDomain.db";
// create a connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to SQLite has been established: delDom");
PreparedStatement prepared = conn.prepareStatement("DELETE FROM Domini WHERE Domain = ?");
prepared.setString(1, dm);
int result = prepared.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
// return false;
}
public static void insertDom(JSONObject j) throws SQLException, JSONException {
// connect();
// Statement statement =conn.createStatement();
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:DBPissirDomain.db";
// create a connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to SQLite has been established.");
/*
* String sql= "SELECT * FROM Utenti"; Statement statement
* =conn.createStatement(); ResultSet result =statement.executeQuery(sql); while
* (result.next()) { String name=result.getString("User");
*
* String desc=result.getString("Desc"); System.out.println(name+desc); }
*/
/*
String dom=j.getString("domain");
String desc=j.getString("desc");
String usr=j.getString("users");
String rsc=j.getString("resources");
String srv=j.getString("services");
String sql = "INSERT INTO Domini (Domain, Desc, Users, Resources, State, Services) VALUES ("+dom+","+desc+","+usr+","+rsc+","+"N/A"+","+srv+")";
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(sql);
*/
PreparedStatement prepared = conn.prepareStatement("INSERT INTO Domini (Domain, Desc, Users, Resources, State, Services) VALUES (?1,?2,?3,?4,?5,?6)");
System.out.println(j.getString("domain"));
System.out.println(j.getString("desc"));
System.out.println(j.getString("users"));
System.out.println(j.getString("services"));
System.out.println(j.getString("resources"));
prepared.setString(1, j.getString("domain").toString().trim());
prepared.setString(2, j.getString("desc").toString().trim());
prepared.setString(3, j.getString("users").toString().trim());
prepared.setString(4, j.getString("resources").toString().trim());
prepared.setString(5, "N/A".toString());
prepared.setString(6, j.getString("services").toString().trim());
int result = prepared.executeUpdate();
//System.out.println(result.toString());
// String sql= "SELECT * FROM Domini WHERE Domain = ?";
// ResultSet result =statement.executeQuery(sql);
// Dominio res=new Dominio();
/*
while (result.next()) {
String dom = result.getString("Domain");
String desc = result.getString("Desc");
// String users=result.getString("Users");
ArrayList<String> users = new ArrayList<String>(Arrays.asList(result.getString("Users").split(",")));
ArrayList<String> reso = new ArrayList<String>(Arrays.asList(result.getString("Resources").split(",")));
// String reso=result.getString("Resources");
String state = result.getString("State");
ArrayList<String> serv = new ArrayList<String>(Arrays.asList(result.getString("Services").split(",")));
// String serv=result.getString("Services");
Dominio d = new Dominio(dom, desc, users, reso, state, serv);
// res.add(d);
}
*/
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
}
/**
* @param args the command line arguments
*/
// public static void main(String[] args) {
// connect();
// }