I've implemented my code so that it returns a response object New Response(Boolean Type,String Type, List clientsinfo = new ArrayList<>() ) . But the response only returns me the last row added "the number of iterations times" in my ArrayList() objects . I don't understand why . I'm trying to add objects taken from my database through a while loop in a while loop but it only adds the last row .I don't understand why and i need some help please. This is the code below :
public Response all_clients_with_accounts() throws SQLException
{
List<Object> clientinfos = new ArrayList<Object>();
String sql1= "select * from client" ;//Clients info
String sql2="select * from datatab1 where clientId =? ";
String sql3="select * from datatab2 where clientId = ?";
try
{
Statement st1= con.createStatement();
ResultSet sqlrs1 = st1.executeQuery(sql1);
PreparedStatement pstmt2 = con.prepareStatement(sql2);
PreparedStatement pstmt3 = con.prepareStatement(sql3);
while(sqlrs1.next())
{
Client client = new Client ();
client.setId(sqlrs1.getInt(1));
client.setNom(sqlrs1.getString(2));
client.setPrenom(sqlrs1.getString(3));
client.setCnib(sqlrs1.getString(4));
client.setContact(sqlrs1.getString(5));
client.setProfession(sqlrs1.getString(6));
client.setVille(sqlrs1.getString(7));
client.setRole(sqlrs1.getString(8));
client.setAgentCode(sqlrs1.getString(9));
client.setDate(sqlrs1.getString(10));
client.setBirthday(sqlrs1.getString(11));
clientaccountinfo.setClient(client);
pstmt2.setInt(1,client.getId());
ResultSet rs2 = pstmt2.executeQuery();
if(rs2.next())
{
Accountclass accountclass = new Accountclass();
accountclass.setTransactionId(rs2.getInt(1));
accountclass.setClientId(rs2.getInt(2));
accountclass.setWallet(rs2.getString(3));
accountclass.setSolde(rs2.getDouble(4));
accountclass.setDebit(rs2.getDouble(5));
accountclass.setCredit(rs2.getDouble(6));
clientaccountinfo.setOrange(accountclass);
}
pstmt3.setInt(1,client.getId());
ResultSet rs3 = pstmt3.executeQuery();
if(rs3.next())
{
Accountclass accountclass = new Accountclass();
accountclass.setTransactionId(rs3.getInt(1));
accountclass.setClientId(rs3.getInt(2));
accountclass.setWallet(rs3.getString(3));
accountclass.setSolde(rs3.getDouble(4));
accountclass.setDebit(rs3.getDouble(5));
accountclass.setCredit(rs3.getDouble(6));
clientaccountinfo.setMobicash(accountclass);
}
clientinfos.add(clientaccountinfo);
}
return new Response(true,"Informations about client",clientinfos);
}
catch (Exception e)
{
e.printStackTrace();
return new Response(false,"Error", null);
}
finally
{
con.close();
}