its my first time asking here instead of read. I'm developing a JAVA software that copies a database into a JSON file. My problem is that I'm only getting copies the last line that gets to database.
Theres my CODE
public void getData() throws IOException {
try {
String query = "select * from Productos";
rs = st.executeQuery(query);
System.out.println("Prueba de funcionamiento");
JSONObject jobj = new JSONObject();
while(rs.next()) {
String cdpt= rs.getString("Cod.RefPro");
String name= rs.getString("Nombre");
String desc = rs.getString("Descripcion");
String prec = rs.getString("PrecioNeto");
String pvp = rs.getString("pvp");
String cdp = rs.getString("CodRefProv");
String fam = rs.getString("Familia");
String exis = rs.getString("Existencias");
System.out.println(cdpt);
JSONArray list = new JSONArray();
JSONObject jobja = new JSONObject();
JSONObject jobjb = new JSONObject();
JSONObject jobjc = new JSONObject();
JSONObject jobjd = new JSONObject();
JSONObject jobje = new JSONObject();
JSONObject jobjf = new JSONObject();
JSONObject jobjg = new JSONObject();
JSONObject jobjh = new JSONObject();
jobja.put("crf", cdpt);
jobjb.put("name", name);
jobjc.put("desc", desc);
jobjd.put("pve", prec);
jobje.put("pvp", pvp);
jobjf.put("cdp", cdp);
jobjg.put("familia", fam);
jobjh.put("existencias", exis);
list.add(jobja);
list.add(jobjb);
list.add(jobjc);
list.add(jobjd);
list.add(jobje);
list.add(jobjf);
list.add(jobjg);
list.add(jobjh);
jobj.put("Productos", list);
}
// try-with-resources statement based on post comment below :)
try (FileWriter file = new FileWriter("N:\\file.json")) {
file.write(jobj.toJSONString());
file.flush();
System.out.println("Successfully Copied JSON Object to File...");
System.out.println("\nJSON Object: " + jobj);
}
}catch(Exception ex) {
System.out.println("Error: "+ex);
}
}
}
I'll be so grateful is someone helps me to solve this problem