How i insert the values into database using Ajax and Json with Struts 2?

How i insert the values into database using Ajax and Json with Struts 2?

sachin_raghavsachin_raghav Posts: 1Questions: 0Answers: 0

I have a jsp form which contains some fields.when i put agent code in textfiels then ALL values retrived from database and then remaining values filled by user and submit form.So all values need to be inserted.but i am unable to insert data into database.
my JS Function is :
function setAgentData() {

var dataObj = {
    "data" : [ {


        'email' : +email,
        'username' : +username,
        'name_app' : +name_app,
        'father_hus_name' : +father_hus_name,
        'current_add' :+current_add,
        'gender' :+gender,
        'dob' : +dob,
        'city' : +city,
        'state' : +state,
        'pan_no' : +pan_no,
        'bank_name' : +bank_name,
        'ifsc_code' :+ifsc_code,
        'nominee_name' :+nominee_name,
        'relation' : +relation,
        'agent_rank'  :+agent_rank,
        'name_oth' : +name_oth,
        'gender_oth' :+gender_oth,
        'rank_oth' : +rank_oth,
        'comp_name_oth' :+comp_name_oth,
        'area_of_work_oth' :+area_of_work_oth,
        'introducer_name' : +introducer_name,
        'introducer_add' :+introducer_add,
        'introducer_rank' : +introducer_rank,
        'full_chain' : +full_chain,
        'agent_code' :+agent_code,
        'age' : +age,
        'pin_code' : +pin_code,
        'mobile_no' : +mobile_no,
        'acc_no' :+acc_no,
        'nominee_age' :+nominee_age,
        'code_oth' : +code_oth,
        'introducer_code' : +introducer_code
    }, ]
};

//var data1 = JSON.stringify(document.getElementByName('email').value);
 //var email = document.getElementById('email').value ;
var data1 = JSON.stringify(dataObj);

$.ajax({
    url : "add_agent.action",
    data : data1,
    dataType : 'json',
    contentType : 'application/json',
    processData: false,
    type : 'POST',
    async : true,
    success : function(res) {
        console.log(res.data.length);
        for(var i=0; i<res.data.length;i++){
            console.log(" "+res.data[i].email+
                    "-"+res.data[i].username+
                    "-"+res.data[i].name_app+
                    "-"+res.data[i].father_hus_name+
                    "-"+res.data[i].current_add+
                    "-"+res.data[i].gender+
                    "-"+res.data[i].dob+
                    "-"+res.data[i].city+
                    "-"+res.data[i].state+
                    "-"+res.data[i].pan_no+
                    "-"+res.data[i].bank_name+
                    "-"+res.data[i].ifsc_code+
                    "-"+res.data[i].nominee_name+
                    "-"+res.data[i].relation+
                    "-"+res.data[i].agent_rank+
                    "-"+res.data[i].name_oth+
                    "-"+res.data[i].gender_oth+
                    "-"+res.data[i].rank_oth+
                    "-"+res.data[i].comp_name_oth+
                    "-"+res.data[i].area_of_work_oth+
                    "-"+res.data[i].introducer_name+
                    "-"+res.data[i].introducer_add+ 
                    "-"+res.data[i].introducer_rank+    
                    "-"+res.data[i].full_chain+ 
                    "-"+res.data[i].agent_code+ 
                    "-"+res.data[i].age+    
                    "-"+res.data[i].pin_code+
                    "-"+res.data[i].mobile_no+
                    "-"+res.data[i].acc_no+
                    "-"+res.data[i].code_oth+
                    "-"+res.data[i].introducer_code

            );

        }
    }
});

console.log("Quite Method ");

}
and my connection method is :
public String execute() throws Exception{

    Report r=new Report();
                try{

        Connection con = ConnectionProvider.getConnection();
        PreparedStatement ps=con.prepareStatement("insert into add_agent (email,username,name_app,father_hus_name,current_add,gender,dob,age,city,state,pin_code,mobile_no,pan_no,acc_no,bank_name,ifsc_code,nominee_name,nominee_age,relation,agent_rank,name_oth,gender_oth,code_oth,rank_oth,comp_name_oth,area_of_work_oth,introducer_code,introducer_name,introducer_add,introducer_rank,full_chain) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

        //*********** Personal Details***********


        ps.setString(1, r.getEmail());
        ps.setString(2, r.getUsername());
        ps.setString(3, r.getName_app());
        ps.setString(4, r.getFather_hus_name());
        ps.setString(5, r.getCurrent_add());
        ps.setString(6, r.getGender());
        ps.setString(7, r.getDob());
        ps.setString(8, r.getAge());
        ps.setString(9, r.getCity());
        ps.setString(10, r.getState());
        ps.setString(11, r.getPin_code());
        ps.setString(12, r.getMobile_no());
        ps.setString(13, r.getPan_no());
        ps.setString(14, r.getAcc_no());
        ps.setString(15, r.getBank_name());
        ps.setString(16, r.getIfsc_code());

        //*********** Nominee  Details***********

        ps.setString(17, r.getNominee_name());
        ps.setString(18, r.getNominee_age());
        ps.setString(19, r.getRelation());
        ps.setString(20, r.getAgent_rank());

         //*********** If Associates Other***********

        ps.setString(21, r.getName_oth());
        ps.setString(22, r.getGender_oth());
        ps.setString(23, r.getCode_oth());
        ps.setString(24, r.getRank_oth());
        ps.setString(25, r.getComp_name_oth());
        ps.setString(26, r.getArea_of_work_oth());

        //*********** Introducer Details***********

        ps.setString(27, r.getIntroducer_code());
        ps.setString(28, r.getIntroducer_name());
        ps.setString(29, r.getIntroducer_rank());
        ps.setString(30, r.getIntroducer_add());
        ps.setString(31, r.getFull_chain());

           int i=        ps.executeUpdate();



           if(i>0){
               return "success";
               }


    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

return "fierror";
}

There is something problem in JS.Please help me out.

Replies

  • ashiersashiers Posts: 101Questions: 8Answers: 7

    Since you're on the Java platform, it just looks like you need to use JED on the server side. JED can simplify your current setup if you just have a look at some of the examples on the JED website: http://jed-datatables.ca/jed/

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    @sachin_raghav - I don't see how your question relates to DataTables? This is a DataTables forum for DataTables questions only, not a general programming forum.

    Allan

This discussion has been closed.