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.