javajsonmethods

Method put()-error in Java while putting data in JsonObject.


I have a question about my error code. I am working on a project with Angular, Java, Spark and an SQL database.

The method contains a prepared statement with an SQL Select statement. In the whileloop wants to go through as long as a result is found. Pack the queried data into a Json object, and these Json objects into a Json array.

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import finanzplanpackage.connection.ConnectionConfiguration;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.String;

import java.util.logging.Level;
import java.util.logging.Logger;



public class n_paresmodul {

    private ConnectionConfiguration c = new ConnectionConfiguration();

    public String viewdata() {
        String data = "";
        JsonArray ja = new JsonArray();
        try{
        PreparedStatement pre = c.getconn().prepareStatement("SELECT * FROM n_pares");
        ResultSet res = pre.executeQuery();
        while (res.next()) {
            JsonObject jo = new JsonObject();
            jo.put("account_id", res.getInt("account_id"));
            jo.put("mandant_id", res.getInt("mandant_id"));
            jo.put("id", res.getInt("id"));
            jo.put("sourcedata", res.getString("sourcedata"));
            ja.add(jo);
        }
        data = ja.toString();
    } catch(
    SQLException ex)

    {
        Logger.getLogger(n_paresmodul.class.getName()).log(Level.SEVERE, null, ex);
    }

    return data;

}

When compiling, the compiler throws me the following error message.

symbol: method put (java. lang. String, int) location: variable jo of type com. google. gson.JsonObject

enter image description here

Can someone please help me and tell me what I'm doing wrong with the put-method?

thank you


Solution

  • com.google.gson.JsonObject does not have any method put()
    You should try using addProperty("account_id", res.getInt("account_id"))