summaryrefslogtreecommitdiff
path: root/src/main/java/controlleur/BDD.java
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2026-07-02 00:14:17 +0200
committerpolo <ordipolo@gmx.fr>2026-07-02 00:14:17 +0200
commit0c2c3ee5a55f36d270171f2f67938542e251fdc5 (patch)
tree8ea4d6a7ad09a5b1389b829980a7a5476982e754 /src/main/java/controlleur/BDD.java
downloadppe_javafx-0c2c3ee5a55f36d270171f2f67938542e251fdc5.tar.gz
ppe_javafx-0c2c3ee5a55f36d270171f2f67938542e251fdc5.tar.bz2
ppe_javafx-0c2c3ee5a55f36d270171f2f67938542e251fdc5.zip
fork utilisant maven
Diffstat (limited to 'src/main/java/controlleur/BDD.java')
-rw-r--r--src/main/java/controlleur/BDD.java322
1 files changed, 322 insertions, 0 deletions
diff --git a/src/main/java/controlleur/BDD.java b/src/main/java/controlleur/BDD.java
new file mode 100644
index 0000000..af1a754
--- /dev/null
+++ b/src/main/java/controlleur/BDD.java
@@ -0,0 +1,322 @@
1package controlleur;
2
3import java.sql.Connection;
4import java.sql.Date;
5import java.sql.DriverManager;
6import java.sql.PreparedStatement;
7import java.sql.ResultSet;
8import java.sql.ResultSetMetaData;
9import java.sql.SQLException;
10import java.sql.Statement;
11import java.util.ArrayList;
12import java.util.List;
13
14import javafx.scene.layout.VBox;
15import modele.Arme;
16import modele.ArmeeListe;
17import modele.Evenement;
18import modele.Unit;
19import modele.User;
20
21public class BDD {
22
23 private Connection connec;
24 private PreparedStatement stat = null;
25 private ResultSet rs ;
26
27 private static String user;
28 private static String password;
29 private static String dbname;
30
31 public BDD() {
32 try {
33 //Class.forName("com.mysql.cj.jdbc.Driver");
34 Class.forName("org.mariadb.jdbc.Driver");
35 //String host = "mysql-stathammer.alwaysdata.net";
36 String host = "localhost";
37 //this.connec = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/"+ dbname, user, password);
38 this.connec = DriverManager.getConnection("jdbc:mariadb://" + host + ":3306/"+ dbname, user, password);
39 this.rs = connec.prepareStatement("SELECT * FROM faction;").executeQuery();
40 } catch (ClassNotFoundException e) {
41 // TODO: handle exception
42
43 e.printStackTrace();
44 } catch (SQLException e) {
45 System.err.println(e.getMessage());
46 }
47 }
48
49 public static void setInfos(String login, String pwd, String base)
50 {
51 user = login;
52 password = pwd;
53 dbname = base;
54 }
55
56 public Statement getStatement() throws SQLException
57 {
58 return connec.createStatement();
59 }
60
61 public PreparedStatement getPreparedStatement(String sql, Object... params) throws SQLException
62 {
63 stat = connec.prepareStatement(sql);
64 if(params.length > 0)
65 {
66 for (int i = 0; i < params.length; i++) {
67 stat.setObject(i + 1, params[i]);
68 }
69 }
70 return stat;
71 }
72
73 public ArrayList<Object> selectUtilisateur(String nom, String mdp) {
74 ArrayList<Object> rendu = new ArrayList<Object>();
75 try {
76
77 String requete = "SELECT id_utilisateur, nom_utilisateur, role_utilisateur FROM utilisateur WHERE nom_utilisateur=? AND mdp_utilisateur = ?;";
78 stat = connec.prepareStatement(requete);
79 stat.setString(1, nom);
80 stat.setString(2,String.valueOf(mdp));
81 //stat.setString(2,String.valueOf(mdp.hashCode()));
82
83 ResultSet rs = stat.executeQuery();
84 ResultSetMetaData md = rs.getMetaData();
85 ArrayList<String> column = new ArrayList<String>();
86
87 for (int i = 1; i <= md.getColumnCount(); i++) {
88 column.add(md.getColumnName(i));
89 }
90 while (rs.next()) {
91 rendu.add(rs.getString("nom_utilisateur"));
92 rendu.add(rs.getInt("id_utilisateur"));
93 rendu.add(rs.getString("role_utilisateur"));
94 }
95 return rendu;
96
97 } catch (SQLException e) {
98 // TODO: handle exception
99 System.err.println(e.getMessage());
100
101 }
102 return rendu;
103 }
104
105 public ArrayList<String> creerUtilisateur(String adresse, String nom, String mdp) {
106 ArrayList<String> crea = new ArrayList<String>();
107
108 String creationRequete = "INSERT INTO utilisateur" + " (nom_utilisateur, email_utilisateur, mdp_utilisateur)"
109 + "VALUES (?, ?, ?);";
110 try {
111 if (connec == null || connec.isClosed()) {
112 System.err.println("La connexion à la bdd n'est pas possible.");
113 return crea;
114 }
115 PreparedStatement stat = connec.prepareStatement(creationRequete);
116 stat.setString(1, nom);
117 stat.setString(2, adresse);
118 stat.setString(3, mdp);
119
120 int rowsInserted = stat.executeUpdate();
121
122 if (rowsInserted > 0) {
123 crea.add(adresse);
124 }
125 stat.close();
126 } catch (SQLException e) {
127 System.err.println("Erreur SQL : " + e.getMessage());
128 }
129 return crea;
130 }
131
132 public Connection getConnection() {
133 return this.connec;
134 }
135
136 public void close() {
137 try {
138 connec.close();
139 } catch (SQLException e) {
140 // TODO Auto-generated catch block
141 e.printStackTrace();
142 }
143 }
144
145 public ArrayList<String> select(String requete,String... param ) throws SQLException{
146 ArrayList<String> rendu = new ArrayList<String>();
147 try {
148 PreparedStatement pstat = null;
149
150 pstat = connec.prepareStatement(requete);
151 if(param.length>0) {
152 for(int i = 1;i<=param.length;i++) {
153 pstat.setString(i, param[i-1]);
154 }
155 }
156
157 rs.close();
158 rs = pstat.executeQuery();
159 ResultSetMetaData md = rs.getMetaData();
160 ArrayList<String> column = new ArrayList<String>();
161
162
163 for(int i =1;i<=md.getColumnCount();i++) {
164 column.add(md.getColumnName(i));
165 }
166 while(rs.next()) {
167 for (String col : column) {
168 rendu.add(rs.getString(col));
169 }
170
171
172
173 }
174
175 return rendu;
176
177 } catch (SQLException e) {
178 // TODO: handle exception
179 System.err.println(e.getMessage());
180
181
182 }
183 return rendu;
184 }
185
186 public void updateUtilisateur(String pseudo,int id) {
187 try {
188 stat = this.getPreparedStatement("UPDATE `utilisateur` SET nom_utilisateur=? WHERE id_utilisateur=?;",pseudo,id);
189 stat.executeUpdate();
190
191 } catch (SQLException e) {
192 // TODO: handle exception
193 System.err.println(e.getMessage());
194 }
195 }
196 public void insertListe(ArmeeListe armee,User session) {
197 try {
198 int id;
199 ArrayList<String> rendu = new ArrayList<String>();
200 String requete = "INSERT INTO liste VALUES(DEFAULT,?,?,?,?);";
201 stat = this.getPreparedStatement(requete);
202 stat.setString(1, armee.getName());
203 stat.setString(2, armee.getDescription());
204 stat.setString(3, armee.getData());
205 stat.setInt(4, session.getId());
206 stat.executeUpdate();
207
208 requete = "SELECT id_liste FROM liste WHERE nom_liste = ?;";
209
210 rendu = this.select(requete,armee.getName());
211
212 id = Integer.parseInt(rendu.getFirst());
213 requete = "INSERT INTO contenir VALUES(?,?);";
214
215 for(Unit unit : armee.getUnits()) {
216
217
218 stat = this.getPreparedStatement(requete);
219 stat.setInt(1, unit.getId());
220 stat.setInt(2, id);
221 stat.executeUpdate();
222 }
223
224 } catch (SQLException e) {
225 // TODO: handle exception
226 }
227 }
228
229
230 public void updateMp(String mdp,int id) {
231 try {
232 //stat = this.getPreparedStatement("UPDATE `utilisateur` SET mdp_utilisateur=? WHERE id_utilisateur=?;",mdp.hashCode(),id);
233 stat = this.getPreparedStatement("UPDATE `utilisateur` SET mdp_utilisateur=? WHERE id_utilisateur=?;",mdp.hashCode(),id);
234 stat.executeUpdate();
235
236 } catch (SQLException e) {
237 // TODO: handle exception
238 System.err.println(e.getMessage());
239 }
240 }
241
242 public int UtilisateurID(String nom, String mdp) {
243 try {
244 stat = this.getPreparedStatement("SELECT id_utilisateur FROM utilisateur WHERE nom_utilisateur=? AND mdp_utilisateur = ?;",
245 nom, mdp);
246
247 ResultSet rs = stat.executeQuery();
248 rs.next();
249 int id = rs.getInt("id_utilisateur");
250
251 return id;
252
253 } catch (SQLException e) {
254 // TODO: handle exception
255 System.err.println(e.getMessage());
256
257 return 0;
258 }
259 }
260 public String UtilisateurRole(String nom, String mdp) {
261 String role = "null";
262 try {
263
264 stat = this.getPreparedStatement("SELECT role_utilisateur FROM utilisateur WHERE nom_utilisateur=? AND mdp_utilisateur = ?;",
265 nom, mdp);
266
267 ResultSet rs = stat.executeQuery();
268 rs.next();
269 role = rs.getString("role_utilisateur");
270
271 return role;
272
273 } catch (SQLException e) {
274 // TODO: handle exception
275 System.err.println(e.getMessage());
276
277
278 }
279 return role;
280 }
281
282 public String UtilisateurMdp(int id) {
283 String mdp = "test null";
284 try {
285 stat = this.getPreparedStatement("SELECT mdp_utilisateur FROM utilisateur WHERE id_utilisateur=?;",
286 id);
287
288 ResultSet rs = stat.executeQuery();
289 rs.next();
290 mdp = rs.getString("mdp_utilisateur");
291
292 return mdp;
293
294 } catch (SQLException e) {
295 // TODO: handle exception
296 System.err.println(e.getMessage());
297
298
299 }
300 return mdp;
301 }
302
303
304 public void ajouter(Arme a) {
305 try {
306 stat.executeUpdate("INSERT INTO arme (id,prenom,login,password,statut,age) VALUES (NULL,");
307 } catch (SQLException e) {
308 // TODO Auto-generated catch block
309 e.printStackTrace();
310 }
311 }
312
313 public void supprimer(Arme a) {
314 try {
315 stat.executeUpdate("DELETE FROM acces WHERE login =''");
316 } catch (SQLException e) {
317 // TODO Auto-generated catch block
318 e.printStackTrace();
319 }
320
321 }
322}