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
|
/* pour tester les classes */
import java.sql.SQLException;
import java.util.HashMap;
import dao.JDBC;
import dao.ResultObject;
import dao.SQLexecutor;
/*
* fonction main pour tester des commandes
*/
public class DAO_Test
{
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
// envoie de tous les paramètres en même temps
JDBC.setInfos("localhost", "mysql", "tp", "root", "");
// même chose avec envoie un par un
// si un paramètre doit être modifié à la volée, faire un JDBC.setStrUlr(); après
JDBC.setHost("localhost");
JDBC.setDBMS("mysql");
JDBC.setDbName("tp");
JDBC.setLogin("root");
JDBC.setPassword("");
JDBC.setStrUrl();
SQLexecutor executor = new SQLexecutor();
// SELECT
ResultObject result = executor.executeQuery("SELECT * FROM acces WHERE id = ?", 1);
for(HashMap<String, Object> row : result.getData()) // foreach sur les entrées
{
System.out.println("prénom: " + row.get("prenom")); //un champ d'une entrée
for(HashMap.Entry<String, Object> one_field : row.entrySet()) // foreach sur les champs
{
System.out.print(one_field.getKey() + ": " + one_field.getValue() + "\n");
}
}
// INSERT
executor.executeQuery("INSERT INTO acces (prenom, login, password, statut, age) VALUES (?, ?, ?, ?, ?)", "Dylan", "Toto", "Titi", "Etudiant", 22);
// UPDATE
executor.executeQuery("UPDATE acces SET statut = ? WHERE prenom = ?", "joue à fortnite", "Dylan");
// DELETE
executor.executeQuery("DELETE FROM acces WHERE prenom = ?", "Dylan");
}
}
|