Bonjour, j'ai une table dans ma DB qui à une attribut de type date.
Code SQL : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 CREATE TABLE IF NOT EXISTS Commandes ( PK_id_commandes int(10) UNSIGNED NOT NULL AUTO_INCREMENT, date_commandes date NOT NULL, cloture_commandes tinyint(1) NOT NULL, montant_commandes double NOT NULL, `annulation _commandes` tinyint(1) NOT NULL, FK_Clients_id_clients int(10) UNSIGNED NOT NULL, PRIMARY KEY (PK_id_commandes)) ENGINE=InnoDB CHARACTER SET UTF8;
Mon test unitaire, envoi une date au format String, comme elle serait rentrée par un utilisateur.
Dans ma Classe Commande quel type attribuer à la date? (String ou Date(java.util)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 @Test public void test1_insert_commande() { Controler contr = Controler.getInstance(); contr.addCommande("2018-10-28", false, 117.24, false, 3); System.out.println("TEST1 - Commande ajouté dans la DB."); }
Quand je met des String, pas de soucis pour récupérer la date et exécuter ma procédure stockée.
Par contre, lorsque je mets l'attribut dateCommande en type Date(java.util), j'ai des erreurs dans ma class DAO.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class Commande { /** * Attributs */ public int idCommande, idClient; public String dateCommande; public Boolean commandeCloturee, commandeAnnulee; public Double montantCommandes; public Client client; /** * Constructeur par défaut */ public Commande() { } /** * Constructeur * @param id */ public Commande(int id) { this.idClient = id; } /** * Constructeur * @param dateCommande * @param commandeCloturee * @param commandeAnnulee * @param montantCommandes * @param client */ public Commande(String dateCommande, Boolean commandeCloturee, Boolean commandeAnnulee, Double montantCommandes, Client client) { this.dateCommande = dateCommande; this.commandeCloturee = commandeCloturee; this.commandeAnnulee = commandeAnnulee; this.montantCommandes = montantCommandes; this.client = client; } /** * Constructeur * @param date * @param cloruree * @param montant * @param annullee * @param idClient */ public Commande(String date, boolean cloruree, double montant, boolean annullee, int idClient) { this.dateCommande = date; this.commandeCloturee = cloruree; this.commandeAnnulee = annullee; this.montantCommandes = montant; this.idClient = idClient; } /** * Accesseurs * @return */ public int getIdCommande() { return idCommande; } public void setIdCommande(int id) { this.idCommande = id; } public String getDateCommande() { return dateCommande; } public void setDateCommande(String dateCommande) { this.dateCommande = dateCommande; } public Boolean getCommandeCloturee() { return commandeCloturee; } public void setCommandeCloturee(Boolean commandeCloturee) { this.commandeCloturee = commandeCloturee; } public Boolean getCommandeAnnulee() { return commandeAnnulee; } public void setCommandeAnnulee(Boolean commandeAnnulee) { this.commandeAnnulee = commandeAnnulee; } public Double getMontantCommandes() { return montantCommandes; } public void setMontantCommandes(Double montantCommandes) { this.montantCommandes = montantCommandes; } public Client getClient() { return client; } public void setClient(Client client) { this.client = client; } public int getIdClient() { return idClient; } public void setIdClient(int idClient) { this.idClient = idClient; } }
Ma question est donc: quel type utiliser pour la date dans ma classe Commande et quel manipulation faire dans mon DAO?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; /** * Opérations CRUD sur les commandes * * */ public class CommandeDAO implements IntDAO { /** * Attribut */ public ArrayList<Object> listCommandes; protected ConnectDB connect = null; /** * Constructeur * * @param con */ public CommandeDAO(ConnectDB con) { this.connect = con; } /** * Create, qui insert une nouvelle commande via une procédure stockée * * @param entity */ @Override public void create(Object entity) { Connection con = null; try { con = ConnectDB.getInstance().getConnection(); con.setAutoCommit(false); String query = "{call commandes_insert(?,?,?,?,?)}"; Commande cmd = (Commande) entity; String date = cmd.getDateCommande(); Boolean cloture = cmd.getCommandeCloturee(); Double montant = cmd.getMontantCommandes(); Boolean annule = cmd.getCommandeAnnulee(); int idClient = cmd.getIdClient(); try (PreparedStatement preparedStmt = con.prepareStatement(query)) { preparedStmt.setString(1, date); preparedStmt.setBoolean(2, cloture); preparedStmt.setDouble(3, montant); preparedStmt.setBoolean(4, annule); preparedStmt.setInt(5, idClient); preparedStmt.executeUpdate(); con.commit(); } } catch (SQLException ex) { if (con != null) { try { con.rollback(); } catch (SQLException ex1) { Logger.getLogger(CommandeDAO.class.getName()).log(Level.SEVERE, null, ex1); JOptionPane.showMessageDialog(null, ex1, "Message d'erreur", JOptionPane.ERROR_MESSAGE); } } Logger.getLogger(CommandeDAO.class.getName()).log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(null, ex, "Message d'erreur", JOptionPane.ERROR_MESSAGE); } finally { try { con.setAutoCommit(true); con.close(); } catch (SQLException ex2) { Logger.getLogger(CommandeDAO.class.getName()).log(Level.SEVERE, null, ex2); JOptionPane.showMessageDialog(null, ex2, "Message d'erreur", JOptionPane.ERROR_MESSAGE); } } }
Sachant, que plus tard, je vais utiliser Swing et un calendrier.
qui utilise ce format de date
Code : Sélectionner tout - Visualiser dans une fenêtre à part new SimpleDateFormat("yyyy-mm--yy
Partager