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
|
/* pour tester les classes */
import java.sql.SQLException;
import java.util.HashMap;
import dao.JDBC;
import dao.ResultObject;
import dao.SQLexecutor;
public class DAO_Test
{
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
JDBC.setInfos("localhost", "mysql", "tp", "root", "");
SQLexecutor executor = new SQLexecutor();
// SELECT
ResultObject result = executor.executeQuery("SELECT * FROM acces WHERE id = ?", 1);
for(HashMap<String, Object> row : result.getData())
{
System.out.println("prenom: " + row.get("prenom")); // une entrée
// foreach sur toutes les entrées
for(HashMap.Entry<String, Object> one_field : row.entrySet())
{
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");
}
}
|