Bonjour,

Il s'agit d'une petite appli où l'utilisateur peut modifier les données du clients, ajouter, supprimer.
Les méthodes ajouter et supprimer marchent, mais pour "modifier" ça ne fonctionne pas.
J'ai beau chercher je ne comprends pas... Si quelqu'un a eu idée.. Merci beaucoup

Classe avec les méthodes
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
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
package com.jassur.dao;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import com.jassur.model.Address;
import com.jassur.model.Client;
import com.mysql.jdbc.Statement;
public class ClientDAO extends DAO<Client> {
	@Override
	public Client create(Client obj) {
		String sql = 
 
				"INSERT INTO clients "+
				"(last_name, first_name, phone, email, business, created_at) "+
				"VALUES (?, ?, ?, ?, ?, ?)";
		try {
			PreparedStatement statement = connect.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			statement.setString(1, obj.getLastName());
			statement.setString(2, obj.getFirstName());
			statement.setString(3, obj.getPhone());
			statement.setString(4, obj.getEmail());
			statement.setBoolean(5, obj.getBusiness());
			statement.setDate(6, new Date(System.currentTimeMillis()));
			int rowInserted = statement.executeUpdate();
			if (rowInserted > 0) {
				ResultSet generatedKeys = statement.getGeneratedKeys();
					if (obj.getAddress() != null) {
					if (generatedKeys.next()) {
						/* Get the generated id for the client and set it */
		                obj.setId(generatedKeys.getInt(1));
		                obj.getAddress().setClientId(obj.getId());
		                AddressDAO addressDAO = new AddressDAO();
		                addressDAO.setConnect(connect);
		                Address newAdd = addressDAO.create(obj.getAddress());
		                obj.setAddress(newAdd);
		            }
				}
				return obj;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
 
	@Override
	public boolean delete(Client obj) {
 
		String sql = "DELETE FROM clients WHERE id_client = "+obj.getId();
 
		System.out.println("SQL REQUEST : "+sql);
 
		try {
 
			int result = this.connect.createStatement().executeUpdate(sql);
 
 
 
			if (result > 0) {
 
				return true;
 
			}
 
			return true;
 
		} catch (Exception e) {
 
			return false;
 
		}
 
	}
	@Override
 
	public Client update(Client obj) {
		// TODO Auto-generated method stub
		String sql = "UPDATE clients SET last_name = ?, first_name = ?, phone = ?, business = ?, email = ?"
				+"WHERE id_client = "+obj.getId();
		System.out.println("SQL REQUEST : "+sql);
		try {
			//int result = this.connect.createStatement().executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
 
			PreparedStatement stateUpdate = connect.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
 
			stateUpdate.setString(1,obj.getLastName());
 
			stateUpdate.setString(2,obj.getFirstName());
 
			stateUpdate.setString(3,obj.getPhone());
 
			stateUpdate.setBoolean(4,obj.getBusiness());
 
			stateUpdate.setString(5,obj.getEmail());
 
			//stateUpdate.setString(5,obj.getCreatedAt());
 
			stateUpdate.executeUpdate(sql);
 
			stateUpdate.close();
 
		//	JOptionPane.showMessageDialog(null, "Modification réalisée",
 
					//"MODIFICATION", JOptionPane.INFORMATION_MESSAGE);
			return obj;
 
		} catch (Exception e) {
 
			e.printStackTrace();
 
			return null;
 
		}
	}
 
	@Override
	public Client find(int id) {
 
		Client client = null;
		try {
 
			ResultSet result = this.connect.createStatement(
 
					ResultSet.TYPE_SCROLL_INSENSITIVE,
 
					ResultSet.CONCUR_READ_ONLY
 
			).executeQuery(
 
					"SELECT * "+
 
				    "FROM clients INNER JOIN addresses ON clients.id_client = addresses.client_id "+
 
					"WHERE clients.id_client = "+id+";");
 
 
 
			if (result.first()) {
 
 
 
				client = new Client();
				client.setId(id);
 
				client.setBusiness(result.getBoolean("business"));
 
				client.setEmail(result.getString("email"));
 
				client.setFirstName(result.getString("first_name"));
 
				client.setLastName(result.getString("last_name"));
 
				client.setPhone(result.getString("phone"));
				Address address = new Address();
 
				address.setCity(result.getString("city"));
 
				address.setStreet(result.getString("street"));
 
				address.setCountry(result.getString("country"));
 
				address.setRegion(result.getString("region"));
 
				address.setZip(result.getInt("zip"));
 
				client.setAddress(address);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return client;
	}
 
	@Override
	public ArrayList<Client> find() {
 
		ArrayList<Client> clients = new ArrayList<Client>();
 
		try {
 
			ResultSet result = this.connect.createStatement(
 
					ResultSet.TYPE_SCROLL_INSENSITIVE,
 
					ResultSet.CONCUR_READ_ONLY
 
			).executeQuery(
 
					"SELECT * "+
 
				    "FROM clients INNER JOIN addresses ON clients.id_client = addresses.client_id ");
 
			if (result.first()) {
				while (result.next()) {
 
					Client client = new Client();
					client.setId(result.getInt("id_client"));
 
					client.setBusiness(result.getBoolean("business"));
 
					client.setEmail(result.getString("email"));
 
					client.setFirstName(result.getString("first_name"));
 
					client.setLastName(result.getString("last_name"));
 
					client.setPhone(result.getString("phone"));
 
					Address address = new Address();
 
					address.setCity(result.getString("city"));
 
					address.setStreet(result.getString("street"));
 
					address.setCountry(result.getString("country"));
 
					address.setRegion(result.getString("region"));
 
					address.setZip(result.getInt("zip"));
					client.setAddress(address);
					clients.add(client);
 
				}		
 
			}
 
		} catch (SQLException e) {
 
			e.printStackTrace();
 
		}
 
		return clients;
 
	}
 
}
classe client
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
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
 
package com.jassur.model;
 
import java.util.ArrayList;
 
 
import org.json.simple.JSONObject;
 
 
public class Client implements Model {
 
 
 
	private int id = 0;
 
 
 
	/*
 
	 * Has one
 
	 */
 
	private Address address = null;
 
private int addressId;
 
 
	/*
 
	 * Has many
 
	 */
 
	private ArrayList<Loan> loans;
 
 
 
	/*
 
	 * Attributes
 
	 */
 
	private String firstName = "";
 
	private String lastName = "";
 
	private String phone = "";
 
private String email = "";
 
private boolean business;
 
 
	/*
 
	 * Constructors
 
	 */
 
	public Client() {
 
		// code ..
 
}
 
 
	public int getId() {
 
		return id;
 
	}
 
	public void setId(int id) {
 
		this.id = id;
 
}
 
	public Address getAddress() {
 
		return address;
 
	}
 
	public void setAddress(Address address) {
 
		this.address = address;
 
}
 
 
	public ArrayList<Loan> getLoans() {
 
		return loans;
 
	}
 
	public void setLoans(ArrayList<Loan> loans) {
 
		this.loans = loans;
 
}
 
public String getFirstName() {
 
		return firstName;
 
	}
 
	public void setFirstName(String firstName) {
 
		this.firstName = firstName;
 
}
 
public String getLastName() {
 
		return lastName;
 
	}
 
	public void setLastName(String lastName) {
 
		this.lastName = lastName;
 
}
 
public String getPhone() {
 
		return phone;
 
	}
 
	public void setPhone(String phone) {
 
		this.phone = phone;
 
}
 
	public String getEmail() {
 
		return email;
 
	}
 
	public void setEmail(String email) {
 
		this.email = email;
 
	}
 
	public boolean getBusiness() {
 
		return business;
 
	}
 
	public void setBusiness(boolean business) {
 
		this.business = business;
 
	}
 
	public int getAddressId() {
 
		return addressId;
 
	}
 
	public void setAddressId(int addressId) {
 
		this.addressId = addressId;
 
	}
 
	@Override
 
	public JSONObject toJSON() {
 
 
 
		JSONObject jObj = new JSONObject();
 
 
 
		jObj.put("id_client", this.id);
 
		jObj.put("first_name", this.firstName);
 
		jObj.put("last_name", this.lastName);
 
		jObj.put("phone", this.phone);
 
		jObj.put("email", this.email);
 
		jObj.put("business", this.business);
 
		if (this.address != null) {
 
			jObj.put("address", this.address.toJSON());
 
		}
		return jObj;
 
	}
	@Override
 
	public void parseJSON(JSONObject jo) {
 
		this.id = (int) (long)jo.get("id_client");
 
		this.firstName = (String)jo.get("first_name");
 
		this.lastName = (String)jo.get("last_name");
 
		this.phone = (String)jo.get("phone");
 
		this.email = (String)jo.get("email");
 
		this.business = (boolean)jo.get("business");
 
 
 
		if (jo.containsKey("address")) {
 
			Address ad = new Address();
 
			ad.parseJSON((JSONObject) jo.get("address"));
 
			this.address = ad;
 
		}
 
 
 
	}
 
 
 
}