-1

I am starting my studies in Java. For this reason I am not very familiar with libraries and methods that exist Java. I would like to understand if there is any way of catching an object of type ResultSet, as the the return of a query made as follows:

The ResultSet rs_Tbl_CONTROL_SYNCHRONISM = 
con.query("Select * From DB_EGLISE.Tbl_CONTROL_synchronism;"); 

After obtaining ResultSet, is there any way to transform the output to JSON format? Is there a library, or some other method that will do this for me?

I noticed a few question similar to my however none of them will respond fully my doubts, must understand how to do in JAVA to transform the return of a ResultSET Object all to JSON, in response found on site, I noticed that it is necessary to inform each field returned, but do not know which fields will return to be a dynamic program, i.e. every time I will make a query. How should I proceed through this fault ? The GSON makes this ? And how do ?

Renan Rodrigues
  • 286
  • 1
  • 4
  • 22
  • I think this topic might help you : [http://stackoverflow.com/questions/6514876/most-effecient-conversion-of-resultset-to-json](http://stackoverflow.com/questions/6514876/most-effecient-conversion-of-resultset-to-json) – Ashenker Jan 29 '16 at 10:41

2 Answers2

1

Yes, you can use Gson adapter to convert ResultSet to Json

Example

List<ObjectX> objects= yourDao.list();
String jsonResult = new Gson().toJson(objects);
Khalil
  • 259
  • 2
  • 8
1

1)firs set the class path to gson-2.1.jar

2)code to conver the ResultSet object information to JSON .

ResultSet rs_Tbl_CONTROL_SYNCHRONISM = con.query("Select * From DB_EGLISE.Tbl_CONTROL_synchronism;");

JsonObject jsonResponse = new JsonObject(); 
JsonArray data = new JsonArray();
while(rs_Tbl_CONTROL_SYNCHRONISM.next() ) {
JsonArray row = new JsonArray();
row.add(new JsonPrimitive(rs_Tbl_CONTROL_SYNCHRONISM.getString("columnName1")));
row.add(new JsonPrimitive(rs_Tbl_CONTROL_SYNCHRONISM.getString("columnName2")));
row.add(new JsonPrimitive(rs_Tbl_CONTROL_SYNCHRONISM.getString("columnName3")));

data.add(row);
}
jsonResponse.add("aaData", data);
Mahesh
  • 1,063
  • 1
  • 12
  • 29
  • How should I proceed with GSON when I do not know the number of columns returned the ResultSET ? – Renan Rodrigues Jan 29 '16 at 11:06
  • count columns ResultSet rs = st.executeQuery(query); ResultSetMetaData rsmd = rs.getMetaData(); int columnsNumber = rsmd.getColumnCount(); – Mahesh Jan 29 '16 at 11:17
  • To add this in my as would be ? The ResultSet rs_Tbl_PEOPLE = con.query("Select * From DB_EGLISE.Tbl_PEOPLE WHERE COD_Ident_IGREJ = '" + w_obj.getString("COD_Ident_IGREJ") +"' and COD_Ident_PERSONS = ''+ w_obj.getString("COD_Ident_persons") +"';"); – Renan Rodrigues Jan 29 '16 at 11:25