0

I am working on a problem, where user fills the form and click on submit button. An ajax call made which sent the parameters to a controller in java.

These are the parameters sent by the client side:

{"fromDate":"2017-09-23T19:00:00Z","toDate":"2017-10-04T18:59:59Z","listA":"'asd'","listB":"'321'","minimumEntry":"1"}

This is my controller class:

@RestController
public class AssociateController {
@Layout(Layout.NONE)
@RequestMapping(value = "save_associate.json", method = RequestMethod.POST)
  public @ResponseBody AjaxResponseBody save_associate(HttpServletRequest request, Model model,@RequestBody final AssociateParams params) throws Exception {

    Connection oracleconn=null;
    DBHelper dbHelper=new DBHelper();
    try {
        oracleconn = dbHelper.getOracleConnection();
        System.out.println("+++ Associate Controller +++");
        System.out.println(params.getAssociateType());
        System.out.println(params.getDatasetName());
        System.out.println(params.getFromDate());
        System.out.println(params.getToDate());
        System.out.println(params.getListA());
        System.out.println(params.getListB());
        System.out.println(params.getMinimumEntry());
    } catch(Exception e){
         e.printStackTrace();
    }
    finally{
         dbHelper.closeDBConnection(oracleconn);
    }
    return null;

  }
}

AssociateParams.java

public class AssociateParams {
    private String associateType;
    private String datasetName;
    private String fromDate;
    private String toDate;
    private String listA;
    private String listB;
    private String minimumEntry;

    public String getAssociateType() {
        return associateType;
    }
    public void setAssociateType(String associateType) {
        this.associateType = associateType;
    }
    public String getDatasetName() {
        return datasetName;
    }
    public void setDatasetName(String datasetName) {
        this.datasetName = datasetName;
    }
    public String getFromDate() {
        return fromDate;
    }
    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }
    public String getToDate() {
        return toDate;
    }
    public void setToDate(String toDate) {
        this.toDate = toDate;
    }
    public String getListA() {
        return listA;
    }
    public void setListA(String listA) {
        this.listA = listA;
    }
    public String getListB() {
        return listB;
    }
    public void setListB(String listB) {
        this.listB = listB;
    }
    public String getMinimumEntry() {
        return minimumEntry;
    }
    public void setMinimumEntry(String minimumEntry) {
        this.minimumEntry = minimumEntry;
    }
}

These parameters can vary in every request. User can send 2 or 3 or 4 and so on .. parameters to the controller. Now i want to count the number of parameters and then get key/value pair from request and save new row per parameter. Lets say user sends above response to controller. Then the code will look like this :

for (int a=0;a<parameters.length;a++) {
   // get key
   // get value
   // INSERT INTO TABLE WHERE ID=key AND VALUE=value   
}

After entering the rows, the db will look like this:

ID              |       VALUE
----------------------------------
fromDate        |   2017-09-23T19:00:00Z
toDate          |   2017-10-04T18:59:59Z
listA           |   asd
listB           |   123
minimunEntry    |   1

How can I insert the received parameters from server into DB like this. I am very new to JAVA, so Please any kind of help will be appreciated.

Noob Player
  • 279
  • 6
  • 25
  • I think, you can use `HashMap` instead of `AssociateParams` in your controller or just create some helper method that can generate iterator or list from `AssociateParams` not null fields – varren Oct 04 '17 at 06:49
  • how can I Iterate on `params` ? – Noob Player Oct 04 '17 at 06:50
  • I mean you can write custom code yourself, just create some list and add field name to it if field value is not null. And then use this list as iterator. If you don't know all the fields(but i think it is not your case) or you want generic solution you can try reflection or some approach from https://stackoverflow.com/q/6796187/1032167 – varren Oct 04 '17 at 07:00

0 Answers0