java - Take results from JDBC and turn into JsonObject -


i trying results jdbc , turn them json objects , finding difficult do.. have

connection con = null; preparedstatement ps = null; resultset rs = null;  try {     con=db.getconnection();     string query = "select city,state zips zip=10012";     ps=con.preparestatement(query);     string line="";     while(rs.next())     {         line=  rs.getarray("city").tostring();     }     jsonobject jsonobj = new jsonobject(line); 

as can see trying results of while loop , put them string variable , put jsonobject.

this not working, have suggestions? i've been looking around , tweaking code , nothing works.

if makes difference using java 1.8 jdk.
database connection works , have tried query within mysql in code.

update: getting blank page when check see output query. reason want in json because supposed restful service android. rs.next() being used loop out of results query.

here subroutine wrote, should solve problem. @ least works me, using postgresql. note creates jsonarray of jsonobjects. if convert string, like: [{"city":"berlin","state":"berlin"},{"city":"tucson","state":"arizona"}, ... ].

 public jsonarray table2json(resultset rs) throws exception {   jsonarray jsarray = new jsonarray();                 int columns = rs.getmetadata().getcolumncount();         while (rs.next()) {             jsonobject obj = new jsonobject();             (int = 1; <= columns; i++) {                 object tempobject=rs.getobject(i);                 obj.put(rs.getmetadata().getcolumnlabel(i), tempobject);             }             jsarray.put(obj);         }         return jsarray; 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -