i'm developping a web service that extract from database all messages, as a json response this is my ws that take the user's password & login, search their ID and extract the user's messages from the data base:
@GET
@Path("/historiquemethod")
@Produces("application/json")
public msgTabl historique(
@QueryParam("pseudo") String pseudo,
@QueryParam("motDePasse") String motDePasse
) {
msgTabl tab = new msgTabl();
MsgBean ms = new MsgBean();
int i = 0;
int id = 0;
// extraire l'id de la personne selon le pseudo & le mot de passe
try {
ResultSet rs1 = conn.createStatement().executeQuery("select id_u from utilisateur where pseudo='" + pseudo + "'and motdepasse='" + motDePasse + "' ");
if (rs1.next()) {
id = rs1.getInt(1);
}
} catch (SQLException ex) {
}
// extraire les messages et les mettre dans le tableau
try {
ResultSet rs2 = conn.createStatement().executeQuery("select * from message where idUser='" +id+ "'");
while (rs2.next()) {
tab.setTest("ok");
String from2 = rs2.getString("fromm");
String contenu2 = rs2.getString("contenu");
String dateenvoi2 = rs2.getString("DateEnvoi");
String numexp2 = rs2.getString("NumExp");
ms.setFrommm(from2);
ms.setContenu(contenu2);
ms.setDateEnvoi(dateenvoi2);
ms.setNumExp(numexp2);
tab.m[i] = ms;
i = i + 1;
}
} catch (SQLException ex) {
tab.setTest("Catch error");
}
return tab;
}
Every message is a "msgbean" which contain some information ( from, to, content, id, idUser, Date...)
and the response of the WS is a "msgTabl" which is an object containgin a table of msgbean and a string,
the problem is that my ws is always returning only the last message !! that means that if the user has 3 messages, only the last message which is returned 3 times!!
{
"msgTabl": {
"m": [
{
"contenu": 3333333333,
"dateEnvoi": 3333333333,
"frommm": 333333333,
"id": 3,
"idu": 1,
"numExp": 33333333333
},
{
"contenu": 3333333333,
"dateEnvoi": 3333333333,
"frommm": 333333333,
"id": 3,
"idu": 1,
"numExp": 33333333333
},
{
"contenu": 3333333333,
"dateEnvoi": 3333333333,
"frommm": 333333333,
"id": 3,
"idu": 1,
"numExp": 33333333333
}
],
"test": "ok"
}
}
could you tell me please where is the error ??? is their any prb with my code?? Also, i would like to know why the msgTabl is present inside the json response?? i mean why i dont get
{
"m": [
{
"contenu": 3333333333,
"dateEnvoi": 3333333333,
"frommm": 333333333,
"id": 3,
"idu": 1,
"numExp": 33333333333
},
{
"contenu": 3333333333,
"dateEnvoi": 3333333333,
"frommm": 333333333,
"id": 3,
"idu": 1,
"numExp": 33333333333
},
{
"contenu": 3333333333,
"dateEnvoi": 3333333333,
"frommm": 333333333,
"id": 3,
"idu": 1,
"numExp": 33333333333
}
],
"test": "ok"
}
directly ?