0

I am trying to get values from database using servlet in which i am using JSON object to send the data to my JSP in my JSP, jQuery handles the receiving data.i am able to get data but just the last value of the database not all the values here is all my code any help is Thankful.

jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#country_id").change(function() {
   var xyz = $("option:selected").val();
 var Myselect = $('#state_ref');
$.getJSON("../Retrive_country?stateadd_1=none",
    {countryREF : xyz } ,function(data){
   $('#state_ref').empty();

       $.each(data, function(index, state){
            $("<option></option>")
                .attr("value", state.stateId).text(state.stateName)
                .appendTo('#state_ref');
       });   
 });//end get          
   });
});

</script>

Servlet :

String sql1 = "SELECT * FROM state WHERE country_ref="+countryref+"                      
PreparedStatement pst1 = db.getConnection().prepareStatement(sql1);
ResultSet j = pst1.executeQuery();
JSONObject obj = new JSONObject();
              pw.println("[");
while (j.next()) {
    state_id = j.getString(1);
    state = j.getString(2);
    country_ref = j.getString(3);
    obj.put("stateId", state_id);
obj.put("stateName", state);
}
 pw.println(obj);
pw.println("]");

JSP:

<div class="span2 clear">
<select name="country_id" id="country_id">
<option>-select-</option>


<option id="blabbb">america</option>
<option id="blabbb">UK</option>
<option id="blabbb">Africa</option>

</select></div>

<div class="span2 clear">
<select name="state_ref" id="state_ref">
<option ></option>
</select></div>

when i try to console.log(data); here is the result

enter image description here

here i am getting only one value(last value of column) instead of getting all the values from database.

3bu1
  • 977
  • 12
  • 30
  • You need to check the data returned from servlet, please log the data to your browser's console by issuing `console.log(datat);` within the first line of the `getJSON` callback. Once you do that please post the results here if you need further assistance. The js part seems to be fine i.e. an example with your code and mock data http://jsfiddle.net/BFWB8/ – melc Jan 22 '14 at 07:55
  • i am trying to get values using JSON OBJECT through servlet, and when i try to console.log(data); it says [Object] 0: Object stateId: "1302" stateName: "ssss" __proto__: Object length: 1 __proto__: Array[0] – 3bu1 Jan 22 '14 at 08:12
  • i think there is some problem with my servlet can u help me in figuring that out thank you. – 3bu1 Jan 22 '14 at 08:16

2 Answers2

0

As shown in the console output you only get one object from the servlet and this is the reason only one option is displayed.

You can construct the json by using JSONObject and JSONArray together e.g. creating json string using JSONObject and JSONArray

or maybe if you modify the concatenation by including commas and printing the object within the loop

e.g. untested

JSONObject obj = new JSONObject();
              pw.println("[");
while (j.next()) {
    state_id = j.getString(1);
    state = j.getString(2);
    country_ref = j.getString(3);
    obj.put("stateId", state_id);
obj.put("stateName", state);
 pw.println(obj+",");
}
pw.println("]");
Community
  • 1
  • 1
melc
  • 11,523
  • 3
  • 36
  • 41
  • @3bu1 please check the results of the sql query. If they are ok then use the `JSONArray` solution, if the concatenation does not work. – melc Jan 22 '14 at 10:23
0
ArrayList<String> statelist = new ArrayList<String>();
            ArrayList<String> stateidlist = new ArrayList<String>();
String sql1 = "SELECT * FROM state WHERE country_ref="+countryref+"                      
PreparedStatement pst1 = db.getConnection().prepareStatement(sql1);
ResultSet j = pst1.executeQuery();
JSONObject obj = new JSONObject();
              pw.println("[");
while (j.next()) {
    state_id = j.getString(1);
    state = j.getString(2);
    country_ref = j.getString(3);
    stateidlist.add(state_id);
                  statelist.add(state);
              }
               obj.put("statelist",statelist);
              obj.put("stateidlist",stateidlist);
pw.println("]");

JQuery:

<script>
$(document).ready(function(){
$("#country_id").change(function() {
   var xyz = $("option:selected").val();
 var Myselect = $('#state_ref');
$.getJSON("../Retrive_country?stateadd_1=none",
    {countryREF : xyz } ,function(data){

     console.log(data);
   $('#state_ref').empty();
       $.each(data, function(index, state){
          for(var prop in state.stateidlist){
              var stateidvalue = state.stateidlist[prop];
              var statelistvalue = state.statelist[prop];
              $("<option></option>")
                .attr("value", stateidvalue).text(statelistvalue)
                .appendTo('#state_ref');

              }

       });   
 });//end get          
   });
});

</script>
3bu1
  • 977
  • 12
  • 30