-1

I am trying to query a database and then convert the resultset obtained into json using the google gson library.While trying todo so, i am getting the error:
class com.mysql.jdbc.JDBC4Connection declares multiple JSON fields named connectionLifecycleInterceptors

I have tried to look for the connectionLifecycleInterceptors variable to change the name however i cant.Other questions in this context are not refering only to superclasses and subclass,while the error i am getting,i suppose comes from the database resultset.

 public String convertToGson(ResultSet results)  throws Exception 
  { //code to convert resultset to gson
    Gson gson = new Gson();
    String gsonstring = gson.toJson(results);///it indicates the error is here
    return gsonstring;
   }

here is the error: java.lang.IllegalArgumentException: class com.mysql.jdbc.JDBC4Connection declares multiple JSON fields named connectionLifecycleInterceptors at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102) at com.google.gson.Gson.getAdapter(Gson.java:458) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:56) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.Gson.toJson(Gson.java:704) at com.google.gson.Gson.toJson(Gson.java:683) at com.google.gson.Gson.toJson(Gson.java:638) at com.google.gson.Gson.toJson(Gson.java:618) at sample.QueryCentre.convertToGson(QueryCentre.java:91) at sample.QueryCentre.showavailablerooms(QueryCentre.java:72) at sample.ServerSide.main(ServerSide.java:51)

1 Answer 1

1

ResultSet is not a regular POJO and you need to iterate over it and manually create POJO or Map instance. After this conversion you can pass list of these instances to serialisation process.

Pseudocode:

List<Map<String, Object>> rows = new ArrayList<>();
while (results.next()) {
    Map<String, Object> mapForRow = createMapFrom(result);
    rows.add(mapForRow);
}
String json = gson.toJson(rows);

Helpfull methods:

See also:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.