JAVA store string Array in postgres and retrieve a JSON data from it -
i'm getting trouble images paths storage in db. if pass paths string array , store in text[] array column in postgresql - receiving text value in response
"item_id":44,"item_phone":"432432423423","item_price":67676,"item_descr":"iojgoewjgoiwejgiewjgojoij","item_images":"{8f8161d3a6bdf6ahrth8fd35725356.jpg,11136408_101535465482739704_1782611644_o.jpg,15032923164db54t54d7ee548.jpg.thumb.jpg}","item_title":"ojreogreoihigjei","item_email":"jfeiwojiejowegj@ijfe" "item_images" array value in " " in response, receive string value. want receive json array/list
so, better way organize images paths storage in db receive data json list/array?
this how works now:
the model receive data post:
public class item { public string title; public string descr; public string phone; public string email; public string images[]; public int price; } this how store values in db:
preparedstatement statement=conn.preparestatement("insert goods (item_title, item_descr, item_email, item_phone, item_images, item_price)\n" + " values ( ? , ? , ? , ? , ? , ? )"); int index=1; statement.setint(index++, item.condo_id); statement.setstring(index++,item.title.trim()); statement.setstring(index++,item.descr.trim()); statement.setstring(index++,item.email.trim()); statement.setstring(index++,item.phone); //assuming string statement.setarray(index++, conn.createarrayof("text", item.images)); statement.setint(index++,item.price); //assuming double int rowsupdated = statement.executeupdate(); this how return values in form of jsonarray:
statement st = conn.createstatement(); resultset rs = st.executequery("query select data db"); jsonarray jsonarray; jsonarray = converttojson(rs); public static jsonarray converttojson(resultset resultset) throws exception { jsonarray jsonarray = new jsonarray(); while (resultset.next()) { int total_rows = resultset.getmetadata().getcolumncount(); jsonobject obj = new jsonobject(); (int = 0; < total_rows; i++) { obj.put(resultset.getmetadata().getcolumnlabel(i + 1) .tolowercase(), resultset.getobject(i + 1)); } jsonarray.put(obj); } return jsonarray; }
you can try modify converttojson() method this
string name = resultset.getmetadata().getcolumnlabel(i + 1) .tolowercase(); int type = resultset.getmetadata().getcolumntype(i); if (java.sql.types.array == type) { obj.put(name, resultset.getarray(i + 1)); } else { obj.put(name, resultset.getobject(i + 1)); }
Comments
Post a Comment