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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
package controlleur;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.layout.VBox;
import modele.Arme;
import modele.ArmeeListe;
import modele.Evenement;
import modele.Unit;
import modele.User;
public class BDD {
private Connection connec;
private PreparedStatement stat = null;
private ResultSet rs ;
private static String user;
private static String password;
private static String dbname;
public BDD() {
try {
//Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName("org.mariadb.jdbc.Driver");
//String host = "mysql-stathammer.alwaysdata.net";
String host = "localhost";
//this.connec = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/"+ dbname, user, password);
this.connec = DriverManager.getConnection("jdbc:mariadb://" + host + ":3306/"+ dbname, user, password);
this.rs = connec.prepareStatement("SELECT * FROM faction;").executeQuery();
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
public static void setInfos(String login, String pwd, String base)
{
user = login;
password = pwd;
dbname = base;
}
public Statement getStatement() throws SQLException
{
return connec.createStatement();
}
public PreparedStatement getPreparedStatement(String sql, Object... params) throws SQLException
{
stat = connec.prepareStatement(sql);
if(params.length > 0)
{
for (int i = 0; i < params.length; i++) {
stat.setObject(i + 1, params[i]);
}
}
return stat;
}
public ArrayList<Object> selectUtilisateur(String nom, String mdp) {
ArrayList<Object> rendu = new ArrayList<Object>();
try {
String requete = "SELECT id_utilisateur, nom_utilisateur, role_utilisateur FROM utilisateur WHERE nom_utilisateur=? AND mdp_utilisateur = ?;";
stat = connec.prepareStatement(requete);
stat.setString(1, nom);
stat.setString(2,String.valueOf(mdp));
//stat.setString(2,String.valueOf(mdp.hashCode()));
ResultSet rs = stat.executeQuery();
ResultSetMetaData md = rs.getMetaData();
ArrayList<String> column = new ArrayList<String>();
for (int i = 1; i <= md.getColumnCount(); i++) {
column.add(md.getColumnName(i));
}
while (rs.next()) {
rendu.add(rs.getString("nom_utilisateur"));
rendu.add(rs.getInt("id_utilisateur"));
rendu.add(rs.getString("role_utilisateur"));
}
return rendu;
} catch (SQLException e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
return rendu;
}
public ArrayList<String> creerUtilisateur(String adresse, String nom, String mdp) {
ArrayList<String> crea = new ArrayList<String>();
String creationRequete = "INSERT INTO utilisateur" + " (nom_utilisateur, email_utilisateur, mdp_utilisateur)"
+ "VALUES (?, ?, ?);";
try {
if (connec == null || connec.isClosed()) {
System.err.println("La connexion à la bdd n'est pas possible.");
return crea;
}
PreparedStatement stat = connec.prepareStatement(creationRequete);
stat.setString(1, nom);
stat.setString(2, adresse);
stat.setString(3, mdp);
int rowsInserted = stat.executeUpdate();
if (rowsInserted > 0) {
crea.add(adresse);
}
stat.close();
} catch (SQLException e) {
System.err.println("Erreur SQL : " + e.getMessage());
}
return crea;
}
public Connection getConnection() {
return this.connec;
}
public void close() {
try {
connec.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArrayList<String> select(String requete,String... param ) throws SQLException{
ArrayList<String> rendu = new ArrayList<String>();
try {
PreparedStatement pstat = null;
pstat = connec.prepareStatement(requete);
if(param.length>0) {
for(int i = 1;i<=param.length;i++) {
pstat.setString(i, param[i-1]);
}
}
rs.close();
rs = pstat.executeQuery();
ResultSetMetaData md = rs.getMetaData();
ArrayList<String> column = new ArrayList<String>();
for(int i =1;i<=md.getColumnCount();i++) {
column.add(md.getColumnName(i));
}
while(rs.next()) {
for (String col : column) {
rendu.add(rs.getString(col));
}
}
return rendu;
} catch (SQLException e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
return rendu;
}
public void updateUtilisateur(String pseudo,int id) {
try {
stat = this.getPreparedStatement("UPDATE `utilisateur` SET nom_utilisateur=? WHERE id_utilisateur=?;",pseudo,id);
stat.executeUpdate();
} catch (SQLException e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
}
public void insertListe(ArmeeListe armee,User session) {
try {
int id;
ArrayList<String> rendu = new ArrayList<String>();
String requete = "INSERT INTO liste VALUES(DEFAULT,?,?,?,?);";
stat = this.getPreparedStatement(requete);
stat.setString(1, armee.getName());
stat.setString(2, armee.getDescription());
stat.setString(3, armee.getData());
stat.setInt(4, session.getId());
stat.executeUpdate();
requete = "SELECT id_liste FROM liste WHERE nom_liste = ?;";
rendu = this.select(requete,armee.getName());
id = Integer.parseInt(rendu.getFirst());
requete = "INSERT INTO contenir VALUES(?,?);";
for(Unit unit : armee.getUnits()) {
stat = this.getPreparedStatement(requete);
stat.setInt(1, unit.getId());
stat.setInt(2, id);
stat.executeUpdate();
}
} catch (SQLException e) {
// TODO: handle exception
}
}
public void updateMp(String mdp,int id) {
try {
//stat = this.getPreparedStatement("UPDATE `utilisateur` SET mdp_utilisateur=? WHERE id_utilisateur=?;",mdp.hashCode(),id);
stat = this.getPreparedStatement("UPDATE `utilisateur` SET mdp_utilisateur=? WHERE id_utilisateur=?;",mdp.hashCode(),id);
stat.executeUpdate();
} catch (SQLException e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
}
public int UtilisateurID(String nom, String mdp) {
try {
stat = this.getPreparedStatement("SELECT id_utilisateur FROM utilisateur WHERE nom_utilisateur=? AND mdp_utilisateur = ?;",
nom, mdp);
ResultSet rs = stat.executeQuery();
rs.next();
int id = rs.getInt("id_utilisateur");
return id;
} catch (SQLException e) {
// TODO: handle exception
System.err.println(e.getMessage());
return 0;
}
}
public String UtilisateurRole(String nom, String mdp) {
String role = "null";
try {
stat = this.getPreparedStatement("SELECT role_utilisateur FROM utilisateur WHERE nom_utilisateur=? AND mdp_utilisateur = ?;",
nom, mdp);
ResultSet rs = stat.executeQuery();
rs.next();
role = rs.getString("role_utilisateur");
return role;
} catch (SQLException e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
return role;
}
public String UtilisateurMdp(int id) {
String mdp = "test null";
try {
stat = this.getPreparedStatement("SELECT mdp_utilisateur FROM utilisateur WHERE id_utilisateur=?;",
id);
ResultSet rs = stat.executeQuery();
rs.next();
mdp = rs.getString("mdp_utilisateur");
return mdp;
} catch (SQLException e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
return mdp;
}
public void ajouter(Arme a) {
try {
stat.executeUpdate("INSERT INTO arme (id,prenom,login,password,statut,age) VALUES (NULL,");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void supprimer(Arme a) {
try {
stat.executeUpdate("DELETE FROM acces WHERE login =''");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|